几天前,位置服务工作得很好。现在,该服务仍将启动,但我不再从LocationListener接收更新。 " OnLocationChanged"永远不会被召唤。
这是该服务的代码:
public class LocationUpdateService extends Service {
private LocationListener locationListener;
private LocationManager locationManager;
private LatLng Location = new LatLng(0,0);
private String best;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Log.i("LocationService", "Called 'onCreate'");
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Intent i = new Intent("location_update");
Location = new LatLng(location.getLatitude(), location.getLongitude());
i.putExtra("Longitude", Location.longitude);
i.putExtra("Latitude", Location.latitude);
sendBroadcast(i);
Log.i("LocationService", location.getLongitude() + ", " + location.getLatitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("LocationService", "Status Changed");
}
@Override
public void onProviderEnabled(String provider) {
Log.i("LocationService", "Provided is Enabled");
}
@Override
public void onProviderDisabled(String provider) {
Log.i("LocationService", "Provider is Disabled");
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
best = locationManager.getBestProvider(crit, false);
//noinspection MissingPermission
locationManager.requestLocationUpdates(best, 10000, 0, locationListener);
}
});
t.start();
}
@Override
public void onDestroy(){
super.onDestroy();
if(locationManager != null){
locationManager.removeUpdates(locationListener);
}
}
}
我拥有清单中包含的所有必要权限。我没有在控制台中收到任何更新消息,但我确实收到了服务至少已启动的日志消息。
编辑:这是我请求运行时权限的方式:
private void StartLocationService() {
Intent LocationService = new Intent(this, LocationUpdateService.class);
startService(LocationService);
}
private boolean runtime_permissions(){
if(Build.VERSION.SDK_INT >= 23 && (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)){
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION},100);
return true;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100){
if ( grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
StartLocationService();
}
else {
runtime_permissions();
}
}
}
答案 0 :(得分:0)
您的目标是Android 6.0或更高版本吗?
如果是这样,将权限添加到清单是不够的,您还需要请求运行时权限。结帐https://developer.android.com/training/permissions/requesting.html
之前可能之前有效,因为你在以前版本的Android上运行了吗?
另外,我认为你不需要在后台线程上运行它。尝试在主线程上调用所有内容,看看会发生什么。我有一种感觉你不应该在UI线程以外的线程上调用startActivity(),但我不完全确定。