以编程方式启用和禁用移动数据的最新更新 [英] Latest update on enabling and disabling mobile data programmatically
问题描述
虽然这是一个重复",但当前的答案已经过时,而且大多数情况下不再适用.我认为如果可能的话,最好在此处提供更新的资源,以节省人们的时间,就像我刚刚所做的那样,研究这个问题.
Although this is a "duplicate", the current answers are out of date and, mostly, no longer apply. I thought it would be good to provide an updated resource here, if possible, to save people time, as I have just done, researching this issue.
我一直在搜索有关能够从应用内启用和禁用移动数据(如果 wifi 不可用)的最新信息.
I've been googling around to see the latest information on being able to enable and disable mobile data from within an app (if wifi is not available).
这是我能找到的最新内容之一:
您是否知道无法再从小部件禁用/启用棒棒糖数据?
This is one of the latest things I can find:
Did you know you can no longer Disable/Enable Data on lollipop from a widget?
有一个答案,我引用:
从来没有它的 API.开发人员通过反射调用该方法来使用一种解决方法.Google 所做的只是关闭了这个漏洞".
There was never an API for it. Developers were using a workaround by calling the method via Reflections. All Google did was close this "exploit".
还有这样的讨论:
替换 setMobileDataEnabled() api
这是 2015 年 2 月.
Replacement for setMobileDataEnabled() api
Which is Feb 2015.
这里有这些问题:
这是在 2010 年提出的,最新的答案是在 2014 年 12 月用单班轮更新的.
This was asked in 2010 and the latest answer was updated with a one liner on Dec 2014.
这是 2012 年接受的答案.
And this, the accepted answer in 2012.
关于这个问题的最新消息是什么?
What's the latest on this issue?
还能做吗?
推荐答案
它在 non-rooted 手机上不起作用,因为他们添加了 MODIFY_PHONE_STATE
权限检查.此权限仅授予系统或签名应用参考-此处.
It wont work on non-rooted phone as they added MODIFY_PHONE_STATE
permission check. This permission is only given to system or signature apps refer-here.
@Override
public void setDataEnabled(boolean enable) {
enforceModifyPermission();
mPhone.setDataEnabled(enable);
}
private void enforceModifyPermission() {
mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
}
这篇关于以编程方式启用和禁用移动数据的最新更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!