我需要ACCESS_WIFI_STATE之类的东西,但是我可以通过3g使用dataTrafiic
作者:互联网
在我的android上,我使用WIFI和3gdata流量如何打开和关闭3g数据,因为它不是无限的…我只需要一个类/功能代码行,即可向我显示TelephonyManager.DATA_TRAFIC为enable = false;
解决方法:
这取决于您正在运行的Android版本.我开发的小部件为Take a look at my code.
您需要的代码行是
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) {
isEnabled = true;
} else {
isEnabled = false;
}
“旧版”方法适用于使用TelephonyManager的2.2之前的android版本.
// 2.2+
private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
public static boolean isMobileDataConnected(Context context) {
try {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("getMobileDataEnabled");
setMobileDataEnabledMethod.setAccessible(true);
return Boolean.valueOf(setMobileDataEnabledMethod.invoke(iConnectivityManager).toString());
} catch (Exception e) {
Log.e("NetSwitcher", e.toString());
}
return false;
}
// pre 2.2
public static void setMobileDataEnabledLegacy(Context context) {
boolean isEnabled = false;
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) {
isEnabled = true;
} else {
isEnabled = false;
}
try {
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
if (isEnabled) {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("disableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
} catch (Exception e) {
Log.e("NetSwitcher", e.toString());
}
}
标签:telephonymanager,permissions,android-wifi,android 来源: https://codeday.me/bug/20191101/1985248.html