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 );
暂时了解这些,欢迎共同讨论