Android系統中,只有系統設置里面有入口開關位置服務。其他的應用應該怎么去開關這個服務呢?
首先,應用需要有系統權限(簽名),在這基礎上,我們就可以通過一些手段來實現這個功能。
這里要注意一點,不通的Android版本的操作方式也不一樣。需要區別對待。
應用加上系統簽名
在manifest
標簽里面,加上android:sharedUserId="android.uid.system"
,然后用系統的簽名給apk簽名,可以放到系統中去編譯,也可以用AndroidStudio
指定簽名文件簽名
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.application"
android:sharedUserId="android.uid.system">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application>
...
</application>
</manifest>
Android 5 到 Android 8
對於這幾個版本,通過修改Settings.Secure
數據庫加上廣播即可實現。
private static boolean updateLocationMode(Context context, int oldMode, int newMode) {
Intent intent = new Intent("com.android.settings.location.MODE_CHANGING");
intent.putExtra("CURRENT_MODE", oldMode);
intent.putExtra("NEW_MODE", newMode);
context.sendBroadcast(intent, android.Manifest.permission.WRITE_SECURE_SETTINGS);
return Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, newMode);
}
/**
* Settings.Secure.LOCATION_MODE_OFF // 關閉
* Settings.Secure.LOCATION_MODE_SENSORS_ONLY // GPS only
* Settings.Secure.LOCATION_MODE_BATTERY_SAVING // 降低GPS上報頻率
* Settings.Secure.LOCATION_MODE_HIGH_ACCURACY // 高精度
*/
public static void setLocationEnabled(Context context, int mode){
int oldMode = Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF);
updateLocationMode(context, oldMode, mode);
}
Android 9
這里需要使用反射,調用LocationManager
的setProviderEnabledForUser
方法來實現
@RequiresApi(api = Build.VERSION_CODES.P)
public static void setProviderEnabledForUser(Context context, String provider, boolean enabled){
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
try{
Field field = UserHandle.class.getDeclaredField("SYSTEM");
field.setAccessible(true);
UserHandle userHandle = (UserHandle) field.get(UserHandle.class);
Method method = LocationManager.class.getDeclaredMethod(
"setProviderEnabledForUser",
String.class,
boolean.class,
serHandle.class);
method.invoke(locationManager, provider, enabled, userHandle);
}catch(Exception e){
Log.e(TAG, "can not setProviderEnabledForUser:(" + provider +"," + enabled +")");
}
}
Android 10以上
和Android 9
類似,只不過調用的方法不一樣,通過調用LocationManager
的setLocationEnabledForUser
方法來實現
@RequiresApi(api = Build.VERSION_CODES.Q)
public static void setLocationEnabledForUser(Context context, boolean enabled){
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
try{
Field field = UserHandle.class.getDeclaredField("SYSTEM");
field.setAccessible(true);
UserHandle userHandle = (UserHandle) field.get(UserHandle.class);
Method method = LocationManager.class.getDeclaredMethod(
"setLocationEnabledForUser",
boolean.class,
UserHandle.class);
method.invoke(locationManager, enabled, userHandle);
}catch(Exception e){
Log.e(TAG, "can not setLocationEnabledForUser:(" + enabled +")");
}
}