如何检查是否移动网络启用/禁用 [英] How to check if mobile network is enabled/disabled
问题描述
我想知道,如果移动网络启用或禁用。
I would like to know if the mobile network is enabled or disabled.
我的应用程序,旨在帮助用户当他接到一个电话,而要做到这一点,我需要上网。因此,我希望用户访问应用程序,第一次如果Wi-Fi具有休眠策略和移动网络被禁用时,显示一个信息框。 (我需要互联网在几毫秒内电话铃声开始后)。
My application is designed to help the user when he receives a phone call, and to do this I need Internet access. Thus, I would like to display an information box when the user access the application for the first time if Wi-Fi has a sleep policy and Mobile network is disabled. (I need Internet within milliseconds after the phone start ringing).
我发现Settings.System.WIFI_SLEEP_POLICY,但我无法找到如何检查的任何信息,如果移动网络被禁止(当Wi-Fi已打开并工作)。
I found Settings.System.WIFI_SLEEP_POLICY, but I can't find any information on how to check if mobile network is disabled (when Wi-Fi is on and working).
任何帮助将是AP preciated!
Any help would be appreciated!
编辑: 问题是,我想知道,如果移动网络用户打开的(而手机可能在当时WiFi接入)。
The problem is that I want to know if mobile network is turned of by the user (while the phone could have WiFi access at the time).
推荐答案
我终于找到了解决办法。显然,并不是所有的手机都有这个选项:
I finally found a solution. Apparently not all phones have this option:
首页>菜单>设置>无线和网络>移动网络(复选框)
Home > Menu > Settings > Wireless & networks > Mobile network (checkbox)
然而,对于那些谁做的,这种方法将工作:
However, for those who do, this method will work:
/**
* @return null if unconfirmed
*/
public Boolean isMobileDataEnabled(){
Object connectivityService = getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) connectivityService;
try {
Class<?> c = Class.forName(cm.getClass().getName());
Method m = c.getDeclaredMethod("getMobileDataEnabled");
m.setAccessible(true);
return (Boolean)m.invoke(cm);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
这篇关于如何检查是否移动网络启用/禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!