Android開啟或者關閉GPS
阿新 • • 發佈:2019-01-25
開啟和關閉gps
//開啟或者關閉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;
}
}
}