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

以编程方式在android中启用/禁用数据连接,android,gprs,invocationtargetexception,Android,Gprs,Invocationtargetexception,我想以编程方式启用/禁用数据连接。我使用了以下代码: void enableInternet(boolean yes) { ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); Method iMthd = null; try { iMthd = ConnectivityManager.class.getDeclared

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

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 level 8构建应用程序。

此代码示例适用于运行姜饼和更高版本的android手机:

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);
}
别忘了将这一行添加到清单文件中

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

@riHaN JiTHiN您的程序在2.3及以上版本中运行良好,但需要在“else”语句中稍作修改:

else
     {
        try {
            iMthd.invoke(iMgr, true);
“true”应更改为“false”

iMthd.invoke(iMgr, false);

InvocationTargetException
上的
e.getMessage()
的结果是什么?附带的异常情况如何?您在什么设备上运行此操作,该设备上安装了什么版本的Android OS。我在三星Galaxy S上运行此操作,Android OS为2.2需要发送数据启用和数据禁用的参数是什么?运行此代码后,我发现java.lang.reflect.InvocationTargetException错误。@ReferenceNot是否找到您确定你添加了权限?其他11人或更多人,包括我在内,让我开始工作。感谢回复,但我已经添加了权限,我正在使用Android 2.2。对于低于2.3的Android版本,我们可以使用java反射方法启用/禁用数据连接。参考