求一种普遍可以用的GPS打开的方法?
//好像不能用,提示权限不够,加上也不行
public void setGpsState()
{
       // 打开GPS
       Settings.Secure.setLocationProviderEnabled(getContentResolver(),
LocationManager.GPS_PROVIDER, true);
}
网上说是需要root或程序安装到System/appprivate void setGpsState2()
{
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try
{
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
}
catch (CanceledException e)
{
e.printStackTrace();
}
}
这种好像2.2以上不可以用 
求一种普遍适用的方法 ,谢谢androidGPS

解决方案 »

  1.   

    请参考如下不在Launcher中实现
    这种方案关键在于对数据库值settings.secure.device_provisioned进行监听,如果发现该值被设置,即可执行我们的代码。对该值进行监听,首先要响应android.intent.action.BOOT_COMPLETED广播,也就是当系统完成启动后就执行。系统启动完成后执行什么动作?要执行对数据库相应值的监听,具体说就是要注册一个observer,对settings.secure.device_provisioned进行监听,具体代码如下:
    context.getContentResolver().registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.DEVICE_PROVISIONED), true, gpsObserver);对数据库值注册监听后,在响应方法里,以上面代码为例,就是gpsObserver中,增加对数据库值的设置即可:
    private ContentObserver gpsObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            if (Settings.Secure.getInt(conText.getContentResolver(), Settings.Secure.DEVICE_PROVISIONED, 0) != 0) {            Settings.Secure.setLocationProviderEnabled(conText.getContentResolver(), LocationManager.NETWORK_PROVIDER, false);
                Settings.Secure.setLocationProviderEnabled(conText.getContentResolver(), LocationManager.GPS_PROVIDER, false);            Intent i = new Intent(ACTION_SET_USE_LOCATION_FOR_SERVICES);
                i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                i.putExtra(EXTRA_DISABLE_USE_LOCATION_FOR_SERVICES, true);
                conText.startActivity(i);
            }
        }
    }
      

  2.   

    请问解决了嘛?能自动打开gps了嘛?
      

  3.   

    ACTION_SET_USE_LOCATION_FOR_SERVICES
    EXTRA_DISABLE_USE_LOCATION_FOR_SERVICES
    这是什么东西,报错啊,
    ACTION_SET_USE_LOCATION_FOR_SERVICES cannot be resolved to a variable
    EXTRA_DISABLE_USE_LOCATION_FOR_SERVICES cannot be resolved to a variable
    这常量到哪里去引用?