2016-09-16 62 views
0

ERROR我越來越:我想改變GPS編程設定的Android,但有崩潰

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);} 
} 

回答

0

你在你的問題中的代碼已停止從Android 4.4以上版本的工作。您將從Kitkat版本java.lang.SecurityException開始引發此異常:權限拒絕:不允許發送廣播android.location.GPS_ENABLED_CHANGE

爲了安全起見,Google開發人員已將上述兩種方法都阻止在以前工作正常的情況下。因此得出的結論是,您無法以編程方式開啓或關閉GPS。

+0

好的,謝謝... –