用于在Android 4.0.4平板电脑中启用数据连接的代码

时间:2013-08-01 06:00:27

标签: android

我希望通过代码启用我的3g DataConnection,无需任何用户交互。一切都必须在背景本身完成。

我尝试了我在谷歌找到的所有代码,但在我的Android平板电脑4.0.4中没有任何效果。 我插入了3g sim卡,重启了设备。但代码不会自动调用Tablet中的DataConnection。我的平板电脑内部存储器名称将为“SDCard2”,因此该代码存在任何问题。 请给我适当的解决方案。 我使用下面的代码:

public static void EnableInternet(Context mycontext)
        {
            try {
                Log.i("Reached Enable", "I am here");
                Process proc;
                try {
                    proc = Runtime.getRuntime().exec( "su" );
                    try {
                        proc.waitFor();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }          
                setMobileDataEnabled(mycontext,true);
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    void turnData(boolean ON)
         {
ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            Method iMthd = null;
            try {
                iMthd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                } catch (Exception e) {
                       } 
            iMthd.setAccessible(false);

            if(ON)
             {

                        try {
                            iMthd.invoke(iMgr, true);
                            Toast.makeText(getApplicationContext(), "Data connection Enabled", Toast.LENGTH_SHORT).show();
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            // dataButton.setChecked(false);
                             Toast.makeText(getApplicationContext(), "IllegalArgumentException", Toast.LENGTH_SHORT).show();

                        } catch (IllegalAccessException e) {
                            // TODO Auto-generated catch block
                            Toast.makeText(getApplicationContext(), "IllegalAccessException", Toast.LENGTH_SHORT).show();

                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            // TODO Auto-generated catch block
                            // dataButton.setChecked(false);
                             Toast.makeText(getApplicationContext(), "InvocationTargetException", Toast.LENGTH_SHORT).show();

                        }

             }
            else
             {
                try {
                    iMthd.invoke(iMgr, true);
                    Toast.makeText(getApplicationContext(), "Data connection Disabled", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                          // dataButton.setChecked(true);
                        Toast.makeText(getApplicationContext(), "Error Disabling Data connection", Toast.LENGTH_SHORT).show();
                                            }
             }}





    boolean switchState(boolean enable) 
    {
        boolean bRes = false;

        // Data Connection mode (only if correctly initialized)
        if (m_telManager != null)
        {
            try
            {
              // Will be used to invoke hidden methods with reflection
                Class cTelMan = null;
                Method getITelephony = null;
                Object oTelephony = null;
                Class cTelephony = null;
                Method action = null;

                // Get the current object implementing ITelephony interface
                cTelMan = m_telManager.getClass();
                getITelephony = cTelMan.getDeclaredMethod("getITelephony");
                getITelephony.setAccessible(true);
                oTelephony = getITelephony.invoke(m_telManager);

                // Call the enableDataConnectivity/disableDataConnectivity method
                // of Telephony object
                cTelephony = oTelephony.getClass();
                if (enable)
                {
                    action = cTelephony.getMethod("enableDataConnectivity");
                }
                else
                {
                    action = cTelephony.getMethod("disableDataConnectivity");
                }
                action.setAccessible(true);
                bRes = (Boolean)action.invoke(oTelephony);
            }
            catch (Exception e)
            {
                bRes = false;
            }
        }        
        return bRes;
    }     





    try {
   ConnectivityManager mgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                Method dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
                dataMtd.setAccessible(true);
                dataMtd.invoke(mgr, true);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

我使用了以下权限,我也使用了Manifest文件:

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /> 
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> 
<uses-permission android:name="android.permission.READ_SYNC_STATS" /> 
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 

1 个答案:

答案 0 :(得分:0)

您需要从connectivityManager对象获取IConnectivityManager类的实例,然后在iConnectivityManager实例上使用setMobileDataEnabled()方法。

尝试以下代码(在Galaxy Y(2.3)和Nexus 4(4.2)上测试):

public void cellularState(boolean shouldEnable) {
    ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        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, shouldEnable);
    } catch(IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

在清单声明中:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>