刷新连接状态更改的Android视图

时间:2012-08-23 21:14:19

标签: android broadcastreceiver connectivity

我有几个屏幕,其中有上传按钮。

我还有检查是否存在3g或wifi连接的功能,以及在没有连接时将按钮设置为灰色的功能,如果有,则设置为绿色。唯一的问题是,这只是在首次创建布局时触发,而是我希望在连接发生变化时触发它。

有办法做到这一点吗?从概念上讲,我想在我的主菜单类中使用广播接收器来触发刷新按钮功能,但我不知道如何做到这一点。

我在这里查看了代码: http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/net/NetworkConnectivityListener.java

以及BroadcastReceiver as inner class的建议,但我没有运气

广播接收器在单独的课程中效果很好,但我想将其整合到各种活动中,我不知道如何继续。

任何方向都会有所帮助

private void updateUI(){
    File[] allDir = DataWriter.getRootDir(profile).listFiles();
    if(allDir==null){
        uploadButton.setBackgroundResource(R.drawable.gray_button);
        uploadButton.setEnabled(false);
        return;
    }

    //MAXIM
    boolean anyfiles = false;
    outerloop:
        for(File dir: allDir){
            if(dir.getName().contains(profile[12])){
                File[] allFiles = dir.listFiles();
                for(File f:allFiles){
                    if(f.isDirectory())continue;
                    anyfiles = true;
                    break outerloop;
                }
            }
        }
    //
    if (!anyfiles) {
        uploadButton.setBackgroundResource(R.drawable.gray_button);
        uploadButton.setEnabled(false);

    } else {
        if (intOpt==0) {
            if(checkWifi()==true)
            {   
                uploadButton.setBackgroundResource(R.drawable.red_button);
                uploadButton.setEnabled(true);
            }
            else {
                //MAXIM-changed blue_button to red_button
                uploadButton.setBackgroundResource(R.drawable.gray_button);
                uploadButton.setEnabled(false);
            }
            //uploadButton.setEnabled(true);
        } 
        else if(intOpt==1)
        {
            if(check3G()||checkWifi())
            {
                uploadButton.setBackgroundResource(R.drawable.red_button);
                uploadButton.setEnabled(true);
            }
            else {
                //MAXIM-changed blue_button to red_button
                uploadButton.setBackgroundResource(R.drawable.gray_button);
                uploadButton.setEnabled(false);
            }
            //uploadButton.setEnabled(true);
        }
        else {
            //MAXIM-changed blue_button to red_button
            uploadButton.setBackgroundResource(R.drawable.gray_button);
            uploadButton.setEnabled(false);
        }
        //uploadButton.setEnabled(true);
    }
}


public boolean checkWifi() {
    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo.State wifi = conMan.getNetworkInfo(1).getState();

    if (wifi == android.net.NetworkInfo.State.CONNECTED) {
        return true;
    } else {
        return false;
    }
}

public boolean check3G() {
    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo.State mobile = conMan.getNetworkInfo(0).getState();
    if (mobile == android.net.NetworkInfo.State.CONNECTED) {
        return true;
    } else {
        return false;
    }
}

基本上我需要在连接发生变化时触发updateUI();

由于

跟进:

我收到以下代码的错误“方法的返回类型”

BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("Network Listener", "Network Type Changed");
        UpdateUI();
    }
};

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);        
registerReceiver(networkStateReceiver, filter); // This is the line that gives me an error

我错过了什么吗?

编辑:更多代码

这是主菜单活动 - 用户可以执行任何操作,更改设置等的屏幕

公共类MenuMainActivity扩展Activity {     private BHBluetoothService mBluetoothService = null;

private String[] profile = null;
private String username = null;
private String password = null;
public static String upass = null; 
public static String uuname = null;
private double[] params = new double[2];
private double[] heart = null;
private double[] stride = null;
private double[] speed = null;
private List<File> files = new ArrayList<File>();
public static int intOpt=0;
private Button baselineButton,exerciseButton,fireTrainButton,
fireSuppButton,emerButton,rehaButton,uploadButton;
private static final int PROGRESS_DIALOG = 0;
private ProgressThread progressThread;
private ProgressDialog progressDialog;
private RadioButton wifiButton, button3g;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
    initializeBluetooth();
    Bundle extras = this.getIntent().getExtras();
    if (extras.containsKey("profile")) {
        try {
            profile = extras.getStringArray("profile");
            username = profile[0];
            password = profile[1];
            upass=password;
            uuname=username;
            initializeUI();

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        finish();
    }    
    if (extras.containsKey("params") && extras.containsKey("heart") && extras.containsKey("stride") && extras.containsKey("speed")) {
        params = extras.getDoubleArray("params");
        heart = extras.getDoubleArray("heart");
        stride = extras.getDoubleArray("stride");
        speed = extras.getDoubleArray("speed");
    }
    updateUI();

}

好;这是主菜单活动的起始代码。

我想触发updateUI();在任何连接状态更改。

1 个答案:

答案 0 :(得分:0)

您可以设置BroadcastReceiver来监听连接警报。查看this SO帖子以获取示例。