android - 以编程方式启用/禁用android中的数据连接

标签 android gprs invocationtargetexception

我想以编程方式启用/禁用数据连接。我使用了以下代码:

void enableInternet(boolean yes)
{
    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(yes)
     {

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

它在模拟器中运行没有任何错误,但是当我尝试在真实设备上运行它时出现“InvocationTargetException”。 我正在使用 API 级别 8 来构建应用程序。

最佳答案

此代码示例应该适用于运行 Gingerbread 及更高版本的安卓手机:

private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
    connectivityManagerField.setAccessible(true);
    final Object connectivityManager = connectivityManagerField.get(conman);
    final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}

不要忘记将此行添加到 list 文件中

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

关于android - 以编程方式启用/禁用android中的数据连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11555366/

相关文章:

Android 和 SQlite - 缺少一列?

java - 通过电话线传输字符串数组

android - 将语音转文本字符串解码为日期和时间

Android PublicKey 到字符串

java-me - 如何在 j2me 中检查互联网可用性

python - 使用GPRS扩展板和AT命令将arduino传感器数据发送到服务器

java - HttpClients.createDefault 错误?

java.lang.reflect.InitationTargetException?

Java:由 MissingResourceException 引起的 InitationTargetException,但它确实存在

android - SharedPreferences 名称是否需要唯一?