打开和关闭gps
需要系统权限
android.permission.WRITE_SECURE_SETTINGS
//打开或者关闭gps
public void openGPS(boolean open) {
if (Build.VERSION.SDK_INT <19) {
Secure.setLocationProviderEnabled(context.getContentResolver(),
LocationManager.GPS_PROVIDER, open);
}else{
if(!open){
Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, android.provider.Settings.Secure.LOCATION_MODE_OFF);
}else{
Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, android.provider.Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
}
}
}
//判断gps是否处于打开状态
public boolean isOpen() {
if (Build.VERSION.SDK_INT <19) {
LocationManager myLocationManager = (LocationManager )mContext.getSystemService(Context.LOCATION_SERVICE);
return myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}else{
int state = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
if(state==Settings.Secure.LOCATION_MODE_OFF){
return false;
}else{
return true;
}
}
}
本文介绍了如何在Android应用中通过代码操作GPS的开关,并讨论了所需的系统权限。了解如何在不同SDK版本下设置location_mode,确保权限合规使用。
1219

被折叠的 条评论
为什么被折叠?



