在服务类中添加权限以获取位置时出现 java.lang.ClassCastException
Posted
技术标签:
【中文标题】在服务类中添加权限以获取位置时出现 java.lang.ClassCastException【英文标题】:java.lang.ClassCastException while Adding Permission in Service class to Fetch Location 【发布时间】:2017-06-10 15:48:51 【问题描述】:我正在尝试使用应该及时更新位置的服务来构建一个简单的位置提供程序,所以我做了这个服务:
public class MyService extends Service
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
private final IBinder mBinder = new LocalBinder();
private Intent intent;
Location mLastLocation;
public class LocalBinder extends Binder
public MyService getServerInstance()
return MyService.this;
public Location getLocation()
return mLastLocation;
private class LocationListener implements android.location.LocationListener
public LocationListener(String provider)
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
@Override
public void onLocationChanged(Location location)
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
Log.d("HELLO",String.valueOf(mLastLocation.getLatitude()));
@Override
public void onProviderDisabled(String provider)
Log.e(TAG, "onProviderDisabled: " + provider);
@Override
public void onProviderEnabled(String provider)
Log.e(TAG, "onProviderEnabled: " + provider);
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
Log.e(TAG, "onStatusChanged: " + provider);
LocationListener[] mLocationListeners = new LocationListener[]
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
@Override
public IBinder onBind(Intent intent)
return mBinder;
@Override
public void onCreate()
Log.e(TAG, "onCreate");
initializeLocationManager();
try
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
catch (java.lang.SecurityException ex)
Log.i(TAG, "fail to request location update, ignore", ex);
catch (IllegalArgumentException ex)
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
try
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
catch (java.lang.SecurityException ex)
Log.i(TAG, "fail to request location update, ignore", ex);
catch (IllegalArgumentException ex)
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
@Override
public void onDestroy()
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null)
for (int i = 0; i < mLocationListeners.length; i++)
try
mLocationManager.removeUpdates(mLocationListeners[i]);
catch (Exception ex)
Log.i(TAG, "fail to remove location listners, ignore", ex);
private void initializeLocationManager()
Log.e(TAG, "initializeLocationManager");
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions((Activity) getApplicationContext(), new String[]android.Manifest.permission.ACCESS_COARSE_LOCATION,
30);
if (mLocationManager == null)
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
我检查粗略位置权限的最后一部分失败了,因为这里是 getAplicationContext() 的转换:
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions((Activity) getApplicationContext(), new String[]android.Manifest.permission.ACCESS_COARSE_LOCATION,
30);
if (mLocationManager == null)
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
当我在我的主要活动上启动服务时,我收到此错误:
06-10 15:33:37.682 28790-28790/com.example.afcosta.inesctec.pt.gpstracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.afcosta.inesctec.pt.gpstracker, PID: 28790
java.lang.RuntimeException: Unable to create service com.example.afcosta.inesctec.pt.gpstracker.MyService: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3201)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
at com.example.afcosta.inesctec.pt.gpstracker.MyService.initializeLocationManager(MyService.java:152)
at com.example.afcosta.inesctec.pt.gpstracker.MyService.onCreate(MyService.java:109)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3191)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
任何提示?
【问题讨论】:
为什么要在服务中检查权限,在启动服务之前检查活动本身 【参考方案1】:只要去掉勾选,正常初始化即可
private void initializeLocationManager()
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null)
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
【讨论】:
以上是关于在服务类中添加权限以获取位置时出现 java.lang.ClassCastException的主要内容,如果未能解决你的问题,请参考以下文章
获取 TypeError:__init__() 缺少 1 个必需的位置参数:尝试在包含条目的子表之后添加父表时出现“on_delete”