在android4.0中开启GPS

我的应用中要开启GPS,一直到android2.3.6版本都运行正常。但是到了android4.0版本以后,就不行了。

不知道为什么?在高版本里应该怎么设置呢?

谢谢

试试下面的代码:

public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(!provider.contains("gps")){ //if gps is disabled
        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")); 
        this.ctx.sendBroadcast(poke);
    }
}

在应用中跳转到GPS设置界面,用户自主打开GPS,这样应该可以的。

你这个ctx是什么 是注册的广播吗

4.0以后怎么强制打开GPS

打开的时候报错,闪退,java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=8290, uid=12333