android 在程序中开启GPS功能
16lz
2021-12-04
android 在程序中开启GPS功能
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
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
更多相关文章
- Android(安卓)5.1 open data flow 数据开启流程
- android studio教程 – 快捷键
- Android—构建安全的Android客户端请求,避免非法请求
- Android使用代码实现RelativeLayout,LinearLayout布局
- Android(安卓)下载文件及写入SD卡
- Android桌面小部件实例 桌面小时钟
- Android(安卓)从代码中打开淘宝和亚马逊、facebook、Message
- Creating a ContextMenu on a ListView
- android弹出对话框大全