android 在程序中开启GPS功能

来源:互联网 发布:win10 未识别的网络 编辑:程序博客网 时间:2024/06/05 05:12
  
  LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);       boolean isEnabled= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);if(!isEnabled){//这段代码可以实现GPS开关状态的切换        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(MainActivity.this, 0, GPSIntent, 0).send();        } catch (CanceledException e) {            e.printStackTrace();        }    //Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);

注释掉的代码,表面上看可以简单的实现开启GPS功能,但是运行后发现后没有权限,即使加了权限依旧提示permission denied
原创粉丝点击