切换移动数据按钮

时间:2013-08-07 13:23:06

标签: android

在我的应用程序中,我有两个切换按钮,一个用于wifi,另一个用于移动数据。应用程序启动时,如果我的WiFi处于打开状态,则切换按钮为ON。但是,如果我的移动数据处于开启状态,切换按钮不显示,它仍然是灰色的(无论WiFi发生了什么)。当我按下它时,它变为绿色,我的移动数据仍然打开......任何想法为什么?

gprs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                try {
                    turnData(isChecked);  //Klasa za ukljucivanje gprsa
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });

移动数据类

void turnData(boolean ON) throws Exception {
Log.i("version:", "Found Gingerbread+");
       final ConnectivityManager conman = (ConnectivityManager) getApplicationContext().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);
}

2 个答案:

答案 0 :(得分:1)

为什么要使用反射?如果我是谷歌,我不是,我会在API中提供保护,这样人们就无法使用低级系统对F进行反射。允许反射这种东西会使系统变得脆弱,以至于没有人能够使用它。

如果查看source tree,您会看到IConnectivityManager甚至不是java类,它是一个aidl资源,这意味着它可能是由本机代码(C / C ++)支持所以我不知道反思会在那里发挥作用。

如果您查看您尝试访问的setMobileDataEnabled方法,则会在ConnectivityManager来源中公开。

/**
 * Sets the persisted value for enabling/disabling Mobile data.
 *
 * @param enabled Whether the user wants the mobile data connection used
 *            or not.
 * @hide
 */
public void setMobileDataEnabled(boolean enabled) {
    try {
        mService.setMobileDataEnabled(enabled);
    } catch (RemoteException e) {
    }
}

我没有使用它,但是为什么试图破解底层服务而不只是使用它?

答案 1 :(得分:0)

你可以尝试this,它适用于KitKat。

public boolean invokeMethod(String methodName, Object[] args) throws Exception {
    ConnectivityManager mcm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    Class ownerClass = mcm.getClass();
    Class[] argsClass = null;
    if (args != null) {
        argsClass = new Class[1];
        argsClass[0] = args.getClass();
    }
    Method method = ownerClass.getMethod(methodName, argsClass);
    return (Boolean)method.invoke(mcm, args);
}

public Object invokeBooleanArgMethod(String methodName, boolean value) throws Exception {
    ConnectivityManager mcm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    Class ownerClass = mcm.getClass();
    Class[]  argsClass = new Class[1];
    argsClass[0] = boolean.class;
    Method method = ownerClass.getMethod(methodName,argsClass);
    return method.invoke(mcm, value);
}

/* use these two method like these */
Object[] arg = null;
try {
    boolean isMobileDataEnable = invokeMethod("getMobileDataEnabled", arg);
    if(!isMobileDataEnable){
        invokeBooleanArgMethod("setMobileDataEnabled", true);
    }
} catch (Exception e) {
    e.printStackTrace();
}

此外,在AndroidManifest.xml中,您需要添加

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