gpt4 book ai didi

android - 我需要类似 ACCESS_WIFI_STATE 的东西,但这样我就可以通过 3g 处理数据流量

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:02 24 4
gpt4 key购买 nike

在我的 Android 上,我使用 WIFI 和 3g 数据流量如何关闭和打开 3g 数据,因为它不是无限的......我只需要一个类/函数代码行,它会告诉我 TelephonyManager.DATA_TRAFIC is enable = false;

最佳答案

这取决于你运行的安卓版本。 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());
}
}

关于android - 我需要类似 ACCESS_WIFI_STATE 的东西,但这样我就可以通过 3g 处理数据流量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com