I am trying to make my GPS run in the background, so it can provide me with new information about where I am while I am doing something else.
我正在尝试让我的GPS在后台运行,因此它可以为我提供有关我在做其他事情时的新信息。
But every-time I run the application it crashes. The error wants me to use Looper.prepare()
, but how can I do that with my GPS?
但每次我运行应用程序时都会崩溃。错误要我使用Looper.prepare(),但我怎么能用我的GPS做到这一点?
public class LocationLoggerService extends Service implements LocationListener {
Location location;
String towers;
static final int uniqueID = 1394885;
Thread runner;
boolean keepRunning;
public LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
keepRunning = true;
runner = new Thread(null, new Runnable() {
public void run() {
startGps();
}
});
runner.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// this service will run until we stop it
return START_STICKY;
}
@Override
public void onLocationChanged(Location location) {
location = locationManager.getLastKnownLocation(towers);
String hey1 = Double.toString(location.getLatitude());
String hey2 = Double.toString(location.getLongitude());
Toast.makeText(this,
"TEST onLocationChanged:" + "Lat: " + hey1 + "Long: " + hey2,
Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
@Override
public void onStatusChanged(String s, int i, Bundle b) {
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
.show();
}
public void startGps() {
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 0, this);
location = locationManager.getLastKnownLocation(towers);
String hey1 = Double.toString(location.getLatitude());
String hey2 = Double.toString(location.getLongitude());
Toast.makeText(this, "StartGPS: Lat: " + hey1 + "Long: " + hey2,
Toast.LENGTH_LONG).show();
}
}
My manifest has the following permissions as shown below:
我的清单具有以下权限,如下所示:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<service
android:name="LocationLoggerService"
android:enabled="true"
android:exported="false"
android:label="LocationLoggerService" />
I am getting this error in the logcat as shown below:
我在logcat中收到此错误,如下所示:
FATAL EXCEPTION: Thread-819
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:209)
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:209)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:687)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:512)
at com.example.rememberforme.LocationLoggerService.startGps(LocationLoggerService.java:81)
at com.example.rememberforme.LocationLoggerService$1.run(LocationLoggerService.java:38)
at java.lang.Thread.run(Thread.java:856)
1 个解决方案
#1
0
Heey,
I see that you are missing the onCommandStart method for your service.
我发现你缺少服务的onCommandStart方法。
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//this service will run until we stop it
return START_STICKY;
}
And did you add all the permissions needed in your manifest file? I'm not sure which one you need but one/or more of these for will do it:
您是否添加了清单文件中所需的所有权限?我不确定你需要哪一个但是其中一个/多个会这样做:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>
#1
0
Heey,
I see that you are missing the onCommandStart method for your service.
我发现你缺少服务的onCommandStart方法。
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//this service will run until we stop it
return START_STICKY;
}
And did you add all the permissions needed in your manifest file? I'm not sure which one you need but one/or more of these for will do it:
您是否添加了清单文件中所需的所有权限?我不确定你需要哪一个但是其中一个/多个会这样做:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>