切换按钮未打开

时间:2013-08-08 07:35:20

标签: android

我找到了一个用于以编程方式打开Mobile Data的代码,但是由于某种原因,当我启动应用程序时,我的切换按钮没有打开(之前我打开了Mobile Data - 仅用于检查按钮是否正常工作)。我想提一下,如果在启动应用程序之前没有打开Mobile Data,那么按钮工作正常。

此代码用于切换按钮:

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

编辑:对于WiFi非常简单,但对于移动数据我不知道为什么切换不在

wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
        wifi.setChecked(wifiManager.isWifiEnabled());

        wifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                wifiManager.setWifiEnabled(isChecked);

            }
        });

1 个答案:

答案 0 :(得分:1)

检查切换按钮,具体取决于互联网连接的现状:

可以重复使用以下功能来检查是否存在网络连接:

public boolean connectivityCheck(){
    CheckConnectivity check = new CheckConnectivity();
    check = new CheckConnectivity();
    return check.checkNow(this.getApplicationContext());
}

这也是 CheckConnectivity.class

package com.yourPackageName;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class CheckConnectivity {

    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileinfo;

    public Boolean checkNow(Context con){

        try{

            connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
            wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            mobileinfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if(wifiInfo.isConnected() || mobileinfo.isConnected()){
                return true;
            }
        }
        catch(Exception e){
            System.out.println("CheckConnectivity Exception: " + e.getMessage());
        }
        return false;
    }
}

onCreate()方法中:

if(connectivityCheck()){
  //There's an internet connection
  toggleButton.setChecked(true);
}
else{
  toggleButton.setChecked(false);
}

干杯: - )