2013-12-12 100 views
3
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
intent.putExtra("enabled", true); 
getBaseContext().sendBroadcast(intent);` 

我使用此代碼來啓用GPS,但它給我這樣的錯誤。如何在Android 4.4中以編程方式啓用/禁用GPS?

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

所以,任何人給我的解決方案。

+3

您不能以編程方式啓用的Android /禁用GPS。您可以要求用戶啓用/禁用它。 – Naddy

回答

5

你永遠不會那樣做。這是API中的錯誤。你現在可以給設置用戶的選擇使用以下意圖:

startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
+0

在壁紙服務中使用此代碼,因此它不能用於打開活動。 – ravi152

+0

Settings.ACTION_LOCATION_SOURCE_SETTINGS其包名? –

0
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); 
    } 
+3

這適用於Android 2.3以下版本。這是一個固定的錯誤 – Jakar

相關問題