2012-06-29 23 views
1

我想切換位置提供的開和關,即全球定位系統和無線定位無法切換GPS開/關和無線定位設置通過System.secure

我加入許可清單中

我的代碼更改無線位置設置是...

Settings.Secure.setLocationProviderEnabled(context.getContentResolver(),provider,true);

每當我運行此代碼logcat的

顯示錯誤

logcat的出把

Caused by: java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS 

我搜索了關於這一點,很多人說

的WRITE_SECURE_SETTINGS權限不提供給該AREN應用不是固件的一部分,因爲安全設置旨在保護不受第三方應用程序的修改

這是真的, 如果是我需要的任何其他方式來實現這一目標,

如果沒有那麼如何使用這個,有沒有在我的代碼的任何錯誤...提前

感謝

* 注:*我定義在不同的類文件這個方法,並從服務

回答

0

的Android Settings.Secure.setLocationProviderEnabled稱這不是直接調用允許從任何其他設置application.so試試這個方法來檢查或啓用GPS:

private void isGPSEnable() { 
     String str = Settings.Secure.getString(getContentResolver(), 
       Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     Log.v("GPS", str); 
     if (str != null) { 
      return str.contains("gps"); 
     } 
     else{ 
      return false; 
     } 
    } 

的啓用/禁用GPS的:

private void turnGPSOnoff(){ 
    try 
    { 
    String provider = Settings.Secure.getString(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")); //SET 3 for gps,3 for bluthooth 
     sendBroadcast(poke); 
    } 
    } 
    catch(Exception e) 
    { 
     Log.d("Location", " exception thrown in enabling GPS "+e); 
    } 
} 
+0

,其只對啓用GPS,我怎麼能禁用GPS ?? – Mani

+0

再次調用相同的方法來禁用GPS相同的代碼工作 –