我想改变gps设置编程在安卓,但有崩溃



ERROR I'm Getting:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=15200, uid=10175

我正在使用这个代码。它在任何版本上都不适合我…

public void turnGPSOn()
{
 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
 intent.putExtra("enabled", true);
 this.ctx.sendBroadcast(intent);
 String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
 if(!provider.contains("gps")){ //if gps is disabled
     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")); 
     this.ctx.sendBroadcast(poke);
 }
public static public void turnGPSOff()
{
String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
    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")); 
    this.ctx.sendBroadcast(poke);}
}

你在你的问题中显示的代码已经从Android 4.4版停止工作。你将触发这个异常从Kitkat版本开始java.lang.SecurityException: Permission Denial: not allowed send broadcast android.location.GPS_ENABLED_CHANGE

出于安全目的,谷歌开发人员已经阻止了以上两种方法,这两种方法以前工作良好。因此,结论是,您不能通过编程启动GPS打开或关闭。

最新更新