Android5.0以上系统的移动网络开关
笔者近期遇到一个非常有意思的bug,贴出来和大家分享下。
那是一个温暖的早晨,阳光晒得人非常舒服。一封bug邮件像一片叶子飘到我的邮箱。
一番交流。笔者确认负责的Widget开关在Android5.0以上系统没有作用。相信非常多做过移动网络开关的朋友都知道。传统的方法是在ConnectivityManager中通过反射两个方法setMobileDataEnabled和getMobileDataEnabled来控制移动网络开和关的。
/**
* Gets the value of the setting for enabling Mobile data.
*
* @return Whether mobile data is enabled.
* @hide
*/
public boolean getMobileDataEnabled() {
try {
return mService.getMobileDataEnabled();
} catch (RemoteException e) {
return true;
}
}
/**
* Sets the persisted value for enabling/disabling Mobile data.
*
* @param enabled Whether the mobile data connection should be
* used or not.
* @hide
*/
public void setMobileDataEnabled(boolean enabled) {
try {
mService.setMobileDataEnabled(enabled);
} catch (RemoteException e) {
}
}
可是打开5.0以上的源代码,这两个方法已经不存在了。
推荐一个不错的在线看源代码的站点。在线源代码网址。
此时老大的提醒帮了我。我打开5.0以上代码的TelephonyMananger类尝试通过反射获取setDataEnabled和getDataEnabled类完毕操作。
源代码
/** @hide */
@SystemApi
public void setDataEnabled(boolean enable) {
try {
getITelephony().setDataEnabled(enable);
} catch (RemoteException e) {
Log.e(TAG, "Error calling ITelephony#setDataEnabled", e);
}
}
/** @hide */
@SystemApi
public boolean getDataEnabled() {
try {
return getITelephony().getDataEnabled();
} catch (RemoteException e) {
Log.e(TAG, "Error calling ITelephony#getDataEnabled", e);
}
return false;
}
我的反射方法:
public void setMobileDataState(Context cxt, boolean mobileDataEnabled) {
TelephonyManager telephonyService = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod)
{
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
}
catch (Exception e) {
LogHelper.v(TAG, "Error setting" + ((InvocationTargetException)e).getTargetException() + telephonyService);
}
}
public boolean getMobileDataState(Context cxt) {
TelephonyManager telephonyService = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod)
{
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
}
catch (Exception e) {
LogHelper.v(TAG, "Error getting" + ((InvocationTargetException)e).getTargetException() + telephonyService);
}
return false;
}
可是报了InvocationTargetException错误。
通过:
((InvocationTargetException)e).getTargetException()
方法得知是运行反射运行时发生例如以下错误:
Error settingjava.lang.SecurityException: Neither user 10240 nor current process has android.permission.MODIFY_PHONE_STATE.android.telephony.TelephonyManager
这个时候老大提醒我把APP移动到system/app下试试。
几番尝试。仍是相同的结果。
这个时候一个我无意的操作,将APP移动到system/priv-app,居然成功了。在这个文件夹下,开关又能够又一次工作了。
当然,我们的应用不可能装在这个文件夹下,仅仅能採取折中的方案。但整个过程还是非常开心的,这个bug算是我第一次发现了不同Android版本号之间源代码的差异,感觉非常有意思。希望对大家有所启示。
Android5.0以上系统的移动网络开关的更多相关文章
- Android5.0网络之ipv6
移动设备的大量兴起势必进一步加强ip地址不足的危机. ipv6或许成为一种比較好的选择方案. ipv6地址的获取分为两种方式:无状态:有状态 无状态:通过接收路由公告(RA)来设置自己的ipv6地址 ...
- android5.0中RecycleView的用法
最近学习了android5.0中新增的一个组件RecycleView,是用来代替当前的listview开发的,是因为在RecycleView中已经有了viewholder缓存,并且不同的item之间可 ...
- Win7系统安装Centos7.0双系统(二)
4.6语言选择
- 给新centos系统虚拟机配置网络服务
记录下今天新建虚拟机的过程吧. 镜像:CentOS-6.3-x86_64-minimal; 虚拟机版本: vm 8.0 LET'S----------------------->GO 手动新建一 ...
- ANDROID5.0触摸屏校准
1.校准原理: 1)首先生成校准用的参数,可以适用tslib生成校准参数,也可以使用校准app生成:使用校准app进行校准对使用者要求比较低,使用者可以不用学习复杂的命令:本文使用app方式 2)生成 ...
- Android5.0之Activity的转场动画
Activity的转场动画很早就有,但是太过于单调,样式也不好看,于是Google在Android5.0之后,又推出的新的转场动画,效果还是非常炫的,今天我们一起来看一下. 1.旧转场动画回顾 首先我 ...
- Android5.0之Toobar的使用
总体上来说,Toolbar的使用可以分为两个方面,一方面是将ToolBar当作ActionBar来用,另一方面就是将Toolbar当成一个单独的控件来用,不过到目前为止我见到的大部分情况都是把Tool ...
- Android5.0新特性:RecyclerView实现上拉加载更多
RecyclerView是Android5.0以后推出的新控件,相比于ListView可定制性更大,大有取代ListView之势.下面这篇博客主要来实现RecyclerView的上拉加载更多功能. 基 ...
- Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException
一.写在前面 最近由于廖子尧忙于自己公司的事情和OkGo(一款专注于让网络请求更简单的网络框架) ,故让LZ 接替维护ImagePicker(一款支持单.多选.旋转和裁剪的图片选择器),也是处理了诸多 ...
随机推荐
- JavaScript中的基础测试题
Java ...
- js特效——自动滚动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Nginx Zabbix
https://www.cnblogs.com/wangxiaoqiangs/archive/2016/04/20/5412111.html
- CF16A Flag
CF16A Flag 题意翻译 题目描述 根据一项新的ISO标准,每一个国家的国旗应该是一个n×m的格子场,其中每个格子最多有10种不同的颜色.并且国旗应该有条纹:旗帜的每一行应包含相同颜色的方块,相 ...
- Windows 8.1硬盘安装Ubuntu 14.04双系统
Windows 8.1硬盘安装Ubuntu 14.04双系统 学习了: http://www.jb51.net/os/windows/298507.html http://www.linuxidc.c ...
- java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol
java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol 完整错误信息: org.dom4j.Document ...
- $(window).load(function(){})跟$(document).ready(function(){})跟$(function(){})区别
1.页面DOM加载完成 2.$(document).ready(function(){}) 的简写是 $(function(){}) 执行 3.图片样式等所有HTML元素加载完毕 4.$(windo ...
- Another app is currently holding the yum lock; waiting for it to exit…
yum被锁定无法使用,错误信息截图如下:解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了
- BZOJ 4289: PA2012 Tax(最短路)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 755 Solved: 240[Submit][Status][Discuss] Descriptio ...
- Codeforces 988F. Rain and Umbrellas
解题思路:动态规划 遍历点i,如果从前一个点i-1走到这个点i不需要伞,则疲劳值不变dp[i] = dp[i-1]. 如果前一个点i-1走到这一个点i需要伞,则从前面找一把伞. 即遍历前面的每个点j, ...