经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android使用代码开关Location服务
来源:cnblogs  作者:hlhuang  时间:2021/2/1 11:33:48  对本文有异议

Android系统中,只有系统设置里面有入口开关位置服务。其他的应用应该怎么去开关这个服务呢?

首先,应用需要有系统权限(签名),在这基础上,我们就可以通过一些手段来实现这个功能。
这里要注意一点,不通的Android版本的操作方式也不一样。需要区别对待。

应用加上系统签名

manifest标签里面,加上android:sharedUserId="android.uid.system",然后用系统的签名给apk签名,可以放到系统中去编译,也可以用AndroidStudio指定签名文件签名

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.application"
  4. android:sharedUserId="android.uid.system">
  5. <uses-permission android:name="android.permission.WAKE_LOCK" />
  6. <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
  7. <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
  8. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  9. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  10. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  11. <application>
  12. ...
  13. </application>
  14. </manifest>

Android 5 到 Android 8

对于这几个版本,通过修改Settings.Secure数据库加上广播即可实现。

  1. private static boolean updateLocationMode(Context context, int oldMode, int newMode) {
  2. Intent intent = new Intent("com.android.settings.location.MODE_CHANGING");
  3. intent.putExtra("CURRENT_MODE", oldMode);
  4. intent.putExtra("NEW_MODE", newMode);
  5. context.sendBroadcast(intent, android.Manifest.permission.WRITE_SECURE_SETTINGS);
  6. return Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE, newMode);
  7. }
  8. /**
  9. * Settings.Secure.LOCATION_MODE_OFF // 关闭
  10. * Settings.Secure.LOCATION_MODE_SENSORS_ONLY // GPS only
  11. * Settings.Secure.LOCATION_MODE_BATTERY_SAVING // 降低GPS上报频率
  12. * Settings.Secure.LOCATION_MODE_HIGH_ACCURACY // 高精度
  13. */
  14. public static void setLocationEnabled(Context context, int mode){
  15. int oldMode = Settings.Secure.getInt(context.getContentResolver(),
  16. Settings.Secure.LOCATION_MODE,
  17. Settings.Secure.LOCATION_MODE_OFF);
  18. updateLocationMode(context, oldMode, mode);
  19. }

Android 9

这里需要使用反射,调用LocationManagersetProviderEnabledForUser方法来实现

  1. @RequiresApi(api = Build.VERSION_CODES.P)
  2. public static void setProviderEnabledForUser(Context context, String provider, boolean enabled){
  3. LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  4. try{
  5. Field field = UserHandle.class.getDeclaredField("SYSTEM");
  6. field.setAccessible(true);
  7. UserHandle userHandle = (UserHandle) field.get(UserHandle.class);
  8. Method method = LocationManager.class.getDeclaredMethod(
  9. "setProviderEnabledForUser",
  10. String.class,
  11. boolean.class,
  12. serHandle.class);
  13. method.invoke(locationManager, provider, enabled, userHandle);
  14. }catch(Exception e){
  15. Log.e(TAG, "can not setProviderEnabledForUser:(" + provider +"," + enabled +")");
  16. }
  17. }

Android 10以上

Android 9类似,只不过调用的方法不一样,通过调用LocationManagersetLocationEnabledForUser方法来实现

  1. @RequiresApi(api = Build.VERSION_CODES.Q)
  2. public static void setLocationEnabledForUser(Context context, boolean enabled){
  3. LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  4. try{
  5. Field field = UserHandle.class.getDeclaredField("SYSTEM");
  6. field.setAccessible(true);
  7. UserHandle userHandle = (UserHandle) field.get(UserHandle.class);
  8. Method method = LocationManager.class.getDeclaredMethod(
  9. "setLocationEnabledForUser",
  10. boolean.class,
  11. UserHandle.class);
  12. method.invoke(locationManager, enabled, userHandle);
  13. }catch(Exception e){
  14. Log.e(TAG, "can not setLocationEnabledForUser:(" + enabled +")");
  15. }
  16. }

原文链接:http://www.cnblogs.com/vectoros/p/14300794.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号