【问题标题】:Can't Switch GPS On/Off and Wireless Location Settings through System.secure无法通过 System.secure 打开/关闭 GPS 和无线定位设置
【发布时间】:2012-06-29 15:59:16
【问题描述】:

我想打开和关闭位置提供程序,即 GPS 和无线定位

我在清单中添加了权限

我更改无线位置设置的代码是...

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 权限不适用于不属于固件的应用,因为安全设置旨在防止第三方应用修改

是真的吗 如果是,我需要任何其他方式来实现这一点,

如果没有那么如何使用它,我的代码中是否有任何错误...

提前致谢

*注意:*我在不同的类文件中定义了这个方法,并从SERVICE中调用它

【问题讨论】:

    标签: android gps location wireless


    【解决方案1】:

    Android Settings.Secure.setLocationProviderEnabled 不允许直接从任何其他应用程序调用设置。因此请尝试通过这种方式检查或启用 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);
         }
     }
    

    【讨论】:

    • 它只用于启用 GPS,如何禁用 GPS..??
    • 再次调用相同的方法来禁用 GPS 对两者的相同代码工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2023-03-29
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多