无法通过System.secure切换GPS开/关和无线定位设置 [英] Can't Switch GPS On/Off and Wireless Location Settings through System.secure

查看:630
本文介绍了无法通过System.secure切换GPS开/关和无线定位设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想切换位置提供的开和关,那就是GPS和无线定位

I want switch Location Providers On and Off, that is GPS and Wireless Location

我在明显增加许可

我的code键更改无线位置设置为...

my code to change wireless location settings is...

Settings.Secure.setLocationProviderEnabled(context.getContentResolver(),供应商,真正的);

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

每当我运行此code logcat的显示误差

whenever I run this code logcat shows error

从logcat中把

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

我搜索了这一点,很多人说:

I searched about this, many person says

该WRITE_SECURE_SETTINGS权限不提供给应用程序不属于固件的一部分,因为安全设置旨在采取措施防止通过第三方应用程序修改

The WRITE_SECURE_SETTINGS permission is not available to apps which aren't part of the firmware because secure settings are designed to be secured against modification by third party apps

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

Is it true, if yes I need any other way to achieve this,

如果没有那么如何使用这个,有没有在我的code任何错误...

if no then how to use this, is there any mistake in my code...

在此先感谢

*的注:* 的我定义在不同的类文件这个方法,并从服务调用此

*Note: * I defined this method in different class file, and call this from SERVICE

推荐答案

Android的 Settings.Secure.setLocationProviderEnabled 不是直接调用其他任何设置application.so尝试允许这种方式检查或启用GPS:

Android Settings.Secure.setLocationProviderEnabled is not allow directly calling the Settings from any other application.so try this way to check or enable 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:

an for enable/disable 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);
     }
 }

这篇关于无法通过System.secure切换GPS开/关和无线定位设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆