我正在尝试让我的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();
}
}
我的清单具有以下权限,如下所示:
<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" />
我在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)
答案 0 :(得分:0)
Heey,
我发现您缺少服务的onCommandStart方法。
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//this service will run until we stop it
return START_STICKY;
}
您是否添加了清单文件中所需的所有权限? 我不确定你需要哪一个,但其中一个/或多个会这样做:
<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"/>