Android使用代码开关Location服务
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 +")");
}
}
Android使用代码开关Location服务的更多相关文章
- Android - 位置定位(Location)服务(Service)类的基本操作
位置定位(Location)服务(Service)类的基本操作 本文地址: http://blog.csdn.net/caroline_wendy 定位服务(Location Service),能够确 ...
- Java 断点下载(下载续传)服务端及客户端(Android)代码
原文: Java 断点下载(下载续传)服务端及客户端(Android)代码 - Stars-One的杂货小窝 最近在研究断点下载(下载续传)的功能,此功能需要服务端和客户端进行对接编写,本篇也是记录一 ...
- Android 常用代码大集合 [转]
[Android]调用字符串资源的几种方法 字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...
- Android学习-- 基于位置的服务 LBS(基于百度地图Android SDK)--定位SDK
原文:Android学习-- 基于位置的服务 LBS(基于百度地图Android SDK)--定位SDK 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.ne ...
- Android开发代码规范(转)
Android开发代码规范 1.命名基本原则 在面向对象编程中,对于类,对象,方法,变量等方面的命名是非常有技巧的.比如,大小写的区分,使用不同字母开头等等.但究其本,追其源,在为一个资源其名称 ...
- Android 使用AIDL调用外部服务
好处:多个应用程序之间建立共同的服务机制,通过AIDL在不同应用程序之间达到数据的共享和数据相互操作, 本文包括: 1 .创建AIDL 服务端.2 .创建AIDL 客户端. 3.客户端调用服务端提供的 ...
- Android应用程序请求SurfaceFlinger服务渲染Surface的过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7932268 在前面一篇文章中,我们分析了And ...
- Android该系统提供的服务--Vibrator(振子)
Android该系统提供的服务--Vibrator(振子) --转载请注明出处:coder-pig Vibrator简单介绍与相关方法: watermark/2/text/aHR0cDovL2Jsb2 ...
- Android ListView分页载入(服务端+android端)Demo
Android ListView分页载入功能 在实际开发中经经常使用到,是每一个开发人员必须掌握的内容,本Demo给出了服务端+Android端的两者的代码,并成功通过了測试. 服务端使用MyEcli ...
随机推荐
- leetcode 274H-index
public int hIndex(int[] citations) { /* 唠唠叨叨说了很多 其实找到一个数h,使得数组中至少有h个数大于等于这个数, 其他N-h个数小于这个数,h可能有多个,求最 ...
- Git 常用命令 【13个命令包含git 90%的操作】
- MongoDb学习(五)--Gridfs--上传下载
版本 <dependency> <groupId>org.springframework.data</groupId> <artifactId>spri ...
- TurtleBot3使用课程-第二节a(北京智能佳)
目录 1.[第3类]LRF(LDS)传感器 2 1.1 传感器包安装 2 1.1.1 传感器端口访问设置 2 1.1.2 运行hlds_laser_publisher节点 2 1.1.3 在RViz中 ...
- c通过ctfshow学习php反序列化
web254 web255 web256 web257 web258 web259 web260 web262 web263 web264 web265 web266 web254 error_rep ...
- Redis学习之路(四)Redis-cluster java api操作
import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;import java.util.Hash ...
- 关于Objects类的getClass方法为什么可以得到子类的地址的思考
这一段时间,总是很纠结为什么Objects中的getClass方法可以返回包含子类地址信息的东西(我不确定返回值类型). 因为在Java中,我们定义的父类,我想破脑袋也想不出怎么可以得到子类的信息. ...
- ICPC Central Russia Regional Contest (CRRC 19)题解
题目连接:https://codeforces.com/gym/102780 寒假第二次训练赛,(某菜依旧是4个小时后咕咕咕),战况还行,个人表现极差(高级演员) A:Green tea 暴力枚举即可 ...
- Navcat连接Mysql报错1521
原因是新版的MySql密码加密算法改变,导致旧版本的Navcat连接报错. 解决方案: 1.升级Navcat 因为只是本地的Mysql,所以没验证这个方法,网传可以,此处不介绍. 2.修改Mysql加 ...
- Go中由WaitGroup引发对内存对齐思考
转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的go的源码时14.4 WaitGroup使用大家都会,但是其中是怎么实现的我们 ...