gpt4 book ai didi

java - Android 2.3 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 21:33:43 26 4
gpt4 key购买 nike

我用这段代码关闭wifi连接和数据连接

public static class LowBatteryReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent){
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

try {
ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conmanClass = Class.forName(conman.getClass().getName());
Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
Object iConnectivityManager = iConnectivityManagerField.get(conman);
Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, false);

} catch (Exception e) {
e.printStackTrace();
}
}
}

该类将在工作方法中被调用,但这并不重要。我有一个带有 android 4.3 的 nexus 4,代码可以工作。同样在 android 4.0.3/4、4.1、4.1.2、4.2.1 和 .4.2.2 中。我使用 ActionbarSherlock 库,所以我可以使用 holo.light anctionbar 也可以预览 android 版本。我的一个使用 android 2.3.6 的 friend 尝试了该应用程序并告诉我崩溃了。我现在看不到任何 logcat,但我认为问题出在上面的代码上。我知道在 android 2.3 中还有另一种关闭 3g 的方法,但我不知道是哪一种。我怎样才能检测到 android 版本并制作类似的东西:如果 android >= 4 使用我发布的代码,如果 android <= 4 使用另一个代码(如果有人能告诉我哪个更好,谢谢)。

编辑:我用 android 2.3 找到了一个数据代码。我要不要做类似的事情

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
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;
}

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);
}

所以如果版本是 >= gingerbread 这就是代码对吗?

最佳答案

使用示例:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
// only for gingerbread and newer versions
}

您还可以按以下方式检查版本 4>= 或 <=4 android。

 if(Build.VERSION.SDK_INT >= 4.0){
//this code will be executed on devices running on DONUT (NOT ICS) or later
}
since constant 4 represents donut: public static final int DONUT = 4;

您可以在Build.VERSION 中找到Android 版本。 .

关于java - Android 2.3 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18866338/

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