2

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

此代码用于切换按钮:

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

            }
        });
4

1 回答 1

1

检查 Toggle 按钮,取决于 Internet 连接的存在:

下面的函数可以重复使用来检查是否有网络连接:

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

干杯:-)

于 2013-08-08T08:23:06.257 回答