移动数据切换按钮 [英] Toggle button for Mobile data

查看:108
本文介绍了移动数据切换按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有两个开关按钮,一个是WiFi和另一个用于移动数据。当应用程序启动,如果我的WiFi打开,切换按钮为开。但是,如果我的移动数据为ON时,切换按钮并不表明,它仍然是灰色(不管做什么用的WiFi发生)。当我preSS它,它就会变成绿色,我的移动数据仍是开...任何想法,为什么?

  gprs.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){            @覆盖
            公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
                尝试{
                    turnData(器isChecked); // Klasa ZA ukljucivanje gprsa
                }赶上(例外五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
            }        });

和类移动数据

 无效turnData(布尔ON)抛出异常{
Log.i(版本,发现姜饼+);
       最后ConnectivityManager赌侠=(ConnectivityManager)getApplicationContext()getSystemService(Context.CONNECTIVITY_SERVICE)。
       最后一类conmanClass =的Class.forName(conman.getClass()的getName());
       最后一个字段iConnectivityManagerField = conmanClass.getDeclaredField(MSERVICE);
       iConnectivityManagerField.setAccessible(真);
       最终目标iConnectivityManager = iConnectivityManagerField.get(骗子);
       最后一类iConnectivityManagerClass =的Class.forName(iConnectivityManager.getClass()的getName());
       最后一个方法setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod(setMobileDataEnabled,Boolean.TYPE);
       setMobileDataEnabledMethod.setAccessible(真);
       setMobileDataEnabledMethod.invoke(iConnectivityManager,ON);
}


解决方案

您可以尝试这个,它为我工作在奇巧。

 公共布尔的InvokeMethod(字符串methodName的,对象[]参数)抛出异常{
    ConnectivityManager MCM =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    类ownerClass = mcm.getClass();
    类[] argsClass = NULL;
    如果(参数!= NULL){
        argsClass =新类[1];
        argsClass [0] = args.getClass();
    }
    方法方法= ownerClass.getMethod(方法名,argsClass);
    回报(布尔)method.invoke(MCM,参数);
}公共对象invokeBooleanArgMethod(字符串methodName中,布尔值)抛出异常{
    ConnectivityManager MCM =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    类ownerClass = mcm.getClass();
    类[] = argsClass新类[1];
    argsClass [0] = boolean.class;
    方法方法= ownerClass.getMethod(方法名,argsClass);
    返回method.invoke(MCM,值);
}/ *使用这两种方法,这样的* /
[对象] ARG = NULL;
尝试{
    布尔isMobileDataEnable = invokeMethod中(getMobileDataEnabled,ARG);
    如果(!isMobileDataEnable){
        invokeBooleanArgMethod(setMobileDataEnabled,真正的);
    }
}赶上(例外五){
    e.printStackTrace();
}

此外,的Andr​​oidManifest.xml ,你需要添加

 <使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>
<使用许可权的android:NAME =android.permission.CHANGE_NETWORK_STATE/>

In my app I am having two toggle buttons, one for wifi and the other one for Mobile Data. When applications start, if my WiFi is on, toggle button is ON. But, if my Mobile Data is ON, toggle button doesn't show that, it's still grey(no matter what's happening with WiFi). When I press it, it becomes green and my Mobile Data is still ON... Any idea why?

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

        });

And the class for Mobile Data

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

解决方案

You can try this, it works for me on 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();
}

Also, in AndroidManifest.xml, you need to add

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

这篇关于移动数据切换按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆