1

I have a couple of screens in which there are upload buttons.

I also have functions to check if there is either a 3g or a wifi connection present and a function that sets the buttons to grey if there is no connection and green if there is. The only problem is that this is only triggered when the layout is first created, but instead I want it to be triggered whenever there is a change in connectivity.

Is there a way to do this? Conceptually I want a broadcast receiver in my main menu class that triggers the refresh button function but I am not sure how to do this.

I went through the code here: http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/net/NetworkConnectivityListener.java

and the suggestions here BroadcastReceiver as inner class but I have had no luck

The broadcast receiver works great when it is a separate class, but I want to integrate it into various activities and I am not sure how to proceed.

Any direction would be helpful

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

Basically I need updateUI(); to be triggered when there is a change in connection.

Thanks

Follow up:

I'm getting the error "Return type for the method is missing" for the following code

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

Am I missing something?

EDIT: More code

This is the main menu activity - a screen where the user can do whatever action they desire, change settings, etc

public class MenuMainActivity extends 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();

}

Well; that's the starting code for the main menu activity.

I want to trigger updateUI(); on any connection state change.

Community
  • 1
  • 1
Avi C
  • 176
  • 1
  • 15
  • Could you post more of your code? What method are you trying to register the receiver in? – Sam Aug 23 '12 at 22:38

1 Answers1

0

You can setup a BroadcastReceiver to listen for connectivity alerts. Check out this SO post for an example.

Community
  • 1
  • 1
Display name
  • 1,109
  • 1
  • 15
  • 31
  • I'm getting the error "Return type for the method is missing" for the following code ' 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);' – Avi C Aug 23 '12 at 21:55
  • My apologies, but I edited my answer shortly after posting it since the original SO post linked was very much pseudocode (which appears to be what you are following). I would check out the newly linked SO post for a better example. – Display name Aug 23 '12 at 21:59
  • I was looking at the code - it looks like there is a class that extends BroadcastReceiver. If I try to place this code in another class - my application will crash. I tried to do this a while ago and had no luck. Any advice – Avi C Aug 23 '12 at 22:03