0

我想通过代码启用我的 3g DataConnection,无需任何用户交互。一切都必须在后台完成。

我尝试了在 Google 中找到的所有代码,但在我的 Android 平板电脑 4.0.4 中没有任何效果。我插入了 3g sim 卡,重新启动了设备。但是代码不会自动调用 Tablet 中的 DataConnection。我的 Tablet 内存名称将是“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();
            }

我也使用了以下权限我清单文件:

<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"/> 
4

1 回答 1

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"/>

于 2013-08-01T09:40:37.740 回答