public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
Context context = getActivity();
String provider = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (provider.contains("gps")){
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
context.sendBroadcast(poke);
}
}
return super.onKeyDown(keyCode, event);
}
我做了一些代码,但无法禁用GPS。如果你有一个代码,请分享它,我希望GPS在应用程序最小化或关闭时关闭,当应用程序启动它将自动打开GPS,我打开它但不能关闭..
答案 0 :(得分:1)
启用和禁用GPS取决于用户。您可以向他显示一个对话框,通知他关于禁用GPS的信息。在这种情况下,在对话框上保留两个按钮 - 一个用于“设置”,另一个用于“确定”或“取消” 类似的东西:
public static void promptForGPS(
final Activity activity)
{
final AlertDialog.Builder builder =
new AlertDialog.Builder(activity);
final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
final String message = "Disable GPS....Message here"
+ " service to find current location. Click OK to go to"
+ " location services settings to let you do so.";
builder.setMessage(message)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
activity.startActivity(new Intent(action));
d.dismiss();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
d.cancel();
}
});
builder.create().show();
}
进一步参考:
Android activate gps with AlertDialog: how to wait for the user to take action?
答案 1 :(得分:0)
您使用的代码是Android系统中的安全漏洞。它以前工作。由于开发人员已修复此问题,因此无法以编程方式启动/停止GPS。