简体   繁体   English

Android 2.3应用崩溃

[英]Application crash with android 2.3

I use this code to turn off the wifi connection and data connection 我使用此代码关闭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();
           }
        }
    }

The class will be called in a method to work but this is not important.. I have a nexus 4 with android 4.3 and the code works. 该类将在一种可以工作的方法中调用,但这并不重要。.我有一个与Android 4.3的nexus 4,并且代码有效。 Also in android 4.0.3/4, 4.1, 4.1.2, 4.2.1 and .4.2.2.. I use an ActionbarSherlock library so i can use an holo.light anctionbar also with previews android versions. 同样在android 4.0.3 / 4、4.1、4.1.2、4.2.1和.4.2.2。中,我使用ActionbarSherlock库,因此我可以在预览Android版本时使用holo.light字幕栏。 A friend of mine with android 2.3.6 tryied the application and tells me has a crash.. I can't see any logcat for now but i think the problem is the code above. 我的一个与android 2.3.6一起的朋友试了试应用程序,并告诉我崩溃了。.我现在看不到任何logcat,但我认为问题是上面的代码。 I know that with android 2.3 there was another way to turn the 3g off but i don't know which. 我知道,使用Android 2.3时,还有另一种方法可以关闭3g,但我不知道该使用哪一种。 How can i detect the android version and make something like : If android >= 4 use the code i posted and if android <=4 use another code (if someone can tell me which is is better thanks). 我如何检测android版本并进行以下操作:如果android> = 4,请使用我发布的代码;如果android <= 4,请使用其他代码(如果有人可以告诉我哪个更好,谢谢)。

EDIT: I found a code for data with android 2.3.. Have i to do something like 编辑:我发现了与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);
}

so if the version is >= gingerbread this will be the code right? 所以如果版本是> = gingerbread,这将是正确的代码吗?

Example how to use it: 示例如何使用它:

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

Also you can check as below for the version 4>= or <=4 android. 您也可以按照以下方式检查版本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;

You can find out the Android version looking at Build.VERSION . 您可以在Build.VERSION中找到Android版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM