在我的 android 上,我使用 WIFI 和 3gdata 流量如何关闭和打开 3g 数据,因为它不是无限的......我只需要一个类/函数行代码,它会告诉我 TelephonyManager.DATA_TRAFIC 是 enable = false;
问问题
612 次
1 回答
4
这取决于您正在运行的 android 版本。查看我开发的小部件的代码。
您需要的代码行是
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());
}
}
于 2012-02-23T15:35:30.567 回答