【发布时间】:2016-01-20 23:03:49
【问题描述】:
我的提醒程序中需要一项服务来重复检查位置,我正在使用此代码执行此操作,但在服务启动时我会强制关闭:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
ll = new Mylocationlistener();
checkPermission("android.permission.ACCESS_FINE_LOCATION",1,0);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
return START_STICKY;
}
private class Mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Toast.makeText(getBaseContext(), "Location changed" + location.getLatitude() + location.getLongitude(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
【问题讨论】:
-
你为什么要使用
Service?你可以简单地在你的MainActivity类中完成这项工作。 -
因为我需要在应用程序关闭时检查位置@TasosMoustakas
-
您确定您的 GPS 已启用,即使应用已关闭?
-
在 avd @TasosMoustakas 中测试应用程序
-
您是否尝试过调试您的应用程序?