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 ...
随机推荐
- 一道java算法题
12个人围成一圈,序号依次从1至12,从序号1开始顺时针依次数,数到7的人退出,下一个再依次从1开始数,求留下来的最后一个人的原始序号. public static void joseph(int[] ...
- TurtleBot3 Waffle (tx2版华夫)(11)建图-karto建图
1)[Remote PC] 启动roscore $ roscore 2)[TurBot3] 启动turbot3 $ roslaunch turbot3_bringup minimal.launch 3 ...
- Element UI组件说明
-<el-card>-查询及展示列表页面-[v-show]属性控制显示隐藏-<el-card class="box-card" >-多标签页面-<el ...
- 手动修复 under-replicated blocks in HDFS
解决方式步骤: 1.进入hdfs的pod kubectl get pod -o wide | grep hdfs kubectl exec -ti hadoop-hdfs-namenode-hdfs1 ...
- MySQL中Exists和In的使用
Exists关键字: exists表示存在,是对外表做loop循环,每次loop循环再对内表(子查询)进行查询,那么因为对内表的查询使用的索引(内表效率高,故可用大表),而外表有多大都需要遍历,不可避 ...
- CQRS与Event Sourcing之浅见
引言 DDD是近年软件设计的热门.CQRS与Event Sourcing作为实施DDD的一种选择,也逐步进入人们的视野.围绕这两个主题,软件开发的大咖[Martin Fowler].[Greg You ...
- 结合MATLAB、Python、R语言,在求得显著差异的边(节点对)之后,怎么画circle图
先来看看成果图: OK,开始画图: 实验背景声明:在脑影像分析中,我们首先构建脑网络,然 ...
- VRay for SketchUp渲染图黑原因及解决方案
很多人都遇到用Vray for SketchUp云渲染的时候,渲染出来的图片是全黑或者是局部是黑色, 这是什么原因呢? 1.有一种情况是,SketchUp的文件储存机制和其他的软件有些不同,它是把模型 ...
- 【MyBatis】MyBatis 多表操作
MyBatis 多表操作 文章源码 一对一查询 需求:查询所有账户信息,关联查询下单用户信息. 注意:因为一个账户信息只能供某个用户使用,所以从查询账户信息出发关联查询用户信息为一对一查询.如果从用户 ...
- Centos 6 下安装 OSSEC-2.8.1 (一)
ossec -2.8.1 安装: ## 1 ) 安装依赖包: RedHat / Centos / Fedora / Amazon Linux yum install -y pcre mysql mys ...