android 打开GPS的几种方式

android 打开GPS的几种方式

1.在讨论打开gps的之前先看下如何检测gps的开关情况:

方式一:

boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

方式二(此方式需要android.permission.WRITE_SECURE_SETTINGS权限,此权限仅限于系统应用,所以需要将app安装特定文件夹(此句待验证)):

boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER );

2.开启 GPS的方法:

方式一:手动的定位到"设置-位置信息访问权限"进行设置.

Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(settingsIntent);

方式二,gps的开关,此方法根据不同值还可设置其他功能(0-wifi,1-brightness,2-sync,3-gps,4-bluetooth),本人的4.2.4版本失效,其他版本未测.

public static void toggleGPS(Context context){

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(context, 0, GPSIntent, 0).send();

} catch (CanceledException e) {

e.printStackTrace();

}

}

方式三,此处同检测gps的方式二,都是通过Setting.Secure来完成,需要android.permission.WRITE_SECURE_SETTINGS权限

Settings.Secure.setLocationProviderEnabled( context.getContentResolver(), LocationManager.GPS_PROVIDER, false );

暂时了解这些,欢迎共同讨论

📚 相关推荐

苏泊尔保温杯怎么样质量好吗,五款性价比最高的保温杯推荐
魔兽怀旧服 现阶段削骨战士的理想圆满配装
董路:我们不去世界杯是对世界杯最大的贡献,不能放任外教的错误
索尼手机玩游戏体验如何?
不老的涡虫 | 来实验室上科学课
《水浒传》里面明明没有“水浒”,为什么叫“水浒传”?