在service
中请求位置更新时,出现此错误。授予位置权限,LocationManager
不为空,Provider
不为空。
LocationManager: [LocationManager] requestLocationUpdates(),
java.lang.RuntimeException
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:1013)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:595)
at com.trackinglibrary.service.DataService.createAndRequestLocationUpdates(DataService.java:759)
at com.trackinglibrary.service.DataService.onStartCommand(DataService.java:252)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3694)
at android.app.ActivityThread.access$1600(ActivityThread.java:202)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:198)
at android.app.ActivityThread.main(ActivityThread.java:6729)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
我在onStartCommand()
内部调用的位置代码:
@SuppressLint("MissingPermission")
private void createAndRequestLocationUpdates() {
try {
if (!TrackThatUtils.isLocationEnabled(getApplicationContext())) {
Log.e(TAG, "Location is disable.");
return;
}
if (locationManager == null) {
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
addProximityAlert();
}
if (locationManager == null || !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.e(TAG, "Location provider is disable.");
return;
}
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.e(TAG, "Location provider is: " + isGPSEnabled);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.e(TAG, "== Error On onConnected() Permission not granted");
//Permission not granted by user so cancel the further execution.
return;
}
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAltitudeRequired(false);
criteria.setSpeedRequired(true);
//setCostAllowed(true): This flag enables LocationManager to exchange “data packet” with
// 3G/4G network base stations in order to get better location
criteria.setCostAllowed(true);
criteria.setBearingRequired(false);
//API level 9 and up
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
String provider = locationManager.getBestProvider(criteria, true);
// locationManager.requestLocationUpdates(gpsFreqInMillis, gpsFreqInDistance, criteria, locationListener, null);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
Log.i(TAG, "request for location updates..");
} catch (SecurityException | IllegalArgumentException e) {
Log.e(TAG, "Exception inside createAndRequestLocationUpdates(): " + e);
}
}
答案 0 :(得分:0)
不要在共享首选项中存储Location类,就我而言,这就是问题所在
答案 1 :(得分:0)
根据documentation RuntimeException
,当调用线程没有Looper时被调用。尝试致电Looper.prepare()
之前先致电LocationManager