无法在Android设备4.1.2上打开/关闭移动数据

时间:2014-02-03 08:02:37

标签: android

我使用下面的代码来打开/关闭移动数据,并且它在具有2.3及以上的Android设备上工作正常。我也在具有操作系统4.2.1的Android设备上测试它并且它工作正常。但是在具有os 4.1.2的设备上抛出异常(java.lang.NoSuchFieldException:mService)。

public boolean repairDataConnection(boolean ON, Context context) 
{
    Log.d(tag,"Repair Data Connection work");
    int bv = Build.VERSION.SDK_INT;
    try 
    {
        if (bv == Build.VERSION_CODES.FROYO)
        {
            Method dataConnSwitchmethod;
            Class<?> telephonyManagerClass;
            Object ITelephonyStub;
            Class<?> ITelephonyClass;

            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            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 (ON) 
            {
                dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
            } 
            else 
            {
                dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
            }
            dataConnSwitchmethod.setAccessible(true);
            dataConnSwitchmethod.invoke(ITelephonyStub);
        } 
        else 
        {
            // Log.e(">>>>","App running on Ginger bread+");
            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, ON);
        }
        return true;
    } 
    catch (Exception e) 
    {
        try {
            Log.d(tag,"Exception...."+e);
        } catch (Exception e1) {
            Log.d(tag,"Exception...."+e1);
        }

    }
    return false;

}

1 个答案:

答案 0 :(得分:0)

今天面临同样的问题。 解决了这个问题:

final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class<?> conmanClass = Class.forName(conman.getClass().getName());
final Method setMobileDataEnabledMethod = conmanClass.getMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(conman, Boolean.valueOf(ON));