2012-04-05 67 views
2

以下代碼在Android 2.x.x版本中運行良好。它成功打開設備中的gps。但問題是它不適用於android版本4.x以上。以編程方式打開/關閉在Android版本4.x及以上版本中的gps

我期待在編程4.x版本起開啓GPS。任何幫助或解決方案?

String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     Utility.print("provider string " + provider); 
     if (!provider.contains("gps")) { // if gps is disabled 
      Utility.print("gps is disabled now 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")); 
      sendBroadcast(poke); 
      Utility.print("gps is disabled now enabled 1"); 
     } else { 
      Utility.print("gps is already enabled"); 
     } 
+0

我見過很多應用程序將用戶導向到GPS設置屏幕,而不是直接設置它。 – alberge 2012-04-05 09:49:40

回答

0

從代碼:

<receiver android:name=".widget.SettingsAppWidgetProvider" android:label="@string/gadget_title"> 

和Android 4.0以上變成這樣:

<receiver android:name=".widget.SettingsAppWidgetProvider" 
      android:label="@string/gadget_title" 
      android:exported="false" 
      android:enabled="@bool/has_powercontrol_widget"> 

增加了android:exported="false"

無論內容提供商可以通過其他 應用程序的組件使用 - 「真」如果可以,和「假」如果不是。如果「false」, 提供程序僅適用於相同應用程序的組件 或具有相同用戶標識的應用程序。默認值是true」。

因此,您的應用程序沒有設置應用程序的一些uid和組件名稱,所以它不起作用。

相關問題