Android 如何在应用程序关闭后删除gps更新
我有一个使用gps的应用程序。除非应用程序(由用户或android操作系统)强制关闭并重新打开,否则它工作正常。然后我似乎无法关闭Android 如何在应用程序关闭后删除gps更新,android,gps,Android,Gps,我有一个使用gps的应用程序。除非应用程序(由用户或android操作系统)强制关闭并重新打开,否则它工作正常。然后我似乎无法关闭gps更新。 这是我的代码: private void registerLocationUpdates() { Intent intent = new Intent(ParkOGuardActivity.class.getName() + ".LOCATION_READY"); pendingIntent = PendingIn
gps
更新。
这是我的代码:
private void registerLocationUpdates() {
Intent intent = new Intent(ParkOGuardActivity.class.getName()
+ ".LOCATION_READY");
pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, intent, 0);
// minimum every 1 minutes, 5 kilometers
this.locationManager.requestLocationUpdates(this.provider, 5000,
300000, pendingIntent);
}
private void cancelLocationUpdates() {
if(pendingIntent != null){
Log.d(TAG,pendingIntent!=null ? "pending is not null" : "pending is null");
this.locationManager.removeUpdates(pendingIntent);
}
}
如果我正在调用cancelLocationUpdates()
方法,这是正常的,但是在重新打开应用程序(强制关闭后)后,PendingContent
为空,我无法删除更新。。。
有什么办法吗?我找到了解决办法。这是一个丑陋的,但它的工作:
private void cancelLocationUpdates() {
if(pendingIntent == null) {
registerLocationUpdates();
}
this.locationManager.removeUpdates(pendingIntent);
}
希望有帮助。在开始以下活动之前,检查GPS是否打开:
LocationManager lm;
boolean gpsOn = false;
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER )) {
launchGPSOptions();
if (!gpsOn) launchGPS();
}
private void launchGPSOptions() {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) {
final Intent poke = new Intent();
gpsOn = true;
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
private void launchGPS() {
// final ComponentName toLaunch = new
// ComponentName("com.android.settings","com.android.settings.SecuritySettings");
final Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// intent.setComponent(toLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 0);
}
在代码中使用LaunchGPS和LaunchGPS选项,如下所示:
LocationManager lm;
boolean gpsOn = false;
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER )) {
launchGPSOptions();
if (!gpsOn) launchGPS();
}
private void launchGPSOptions() {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (!provider.contains("gps")) {
final Intent poke = new Intent();
gpsOn = true;
poke.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
private void launchGPS() {
// final ComponentName toLaunch = new
// ComponentName("com.android.settings","com.android.settings.SecuritySettings");
final Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// intent.setComponent(toLaunch);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 0);
}
我认为您错误地命名了这些方法-launchGPSOptions应该是launchGPS,反之亦然。对于任何阅读“launchGPSOptions”的人来说,安卓系统的最新版本是否修补了一个安全漏洞?您无法通过编程启用GPS-您只能启动选项并让用户启用它(此处的“launchGPS()”)