DeimosPhx
DeimosPhx

Reputation: 89

On connection change state broadcast

Before telling me my thread has already been answered I use a solution find on this forum but somehow it doesn't work for me..

My problem: I want to change an image when network connection is on or off

So to do that i did a connectivityReceiver class extending BroadcastReceiver that shoul notify my main class on network state changement.

public class ConnectivityReceiver
    extends BroadcastReceiver {

public static ConnectivityReceiverListener connectivityReceiverListener;

public ConnectivityReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent arg1) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (connectivityReceiverListener != null) {
        connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
    }
}

public static boolean isConnected() {
    ConnectivityManager
            cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();
}


public interface ConnectivityReceiverListener {
    void onNetworkConnectionChanged(boolean isConnected);
}
}

This does work when I call a manual check of the network state but it looks like I can't get my MainActivity to be notified when network change state.

MainActivity (part of the code):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    username_inputed = false;
    responseFromRequest = "";
    username_input = (EditText) findViewById(R.id.username_input);
    confirm_usrname = (ImageButton) findViewById(R.id.next_btn_username);
    passwd_input = (EditText) findViewById(R.id.password_input);
    login = (ImageButton) findViewById(R.id.next_btn_passwd);
    errorLog = (TextView) findViewById(R.id.logError);
    wifi_disp = (ImageView) findViewById(R.id.wifi_prompt);
    checkConnection();
}
private void checkConnection(){
    boolean isConnected = ConnectivityReceiver.isConnected();
    setWifiDisplay(isConnected);
}
@Override
protected void onResume(){
    super.onResume();
    //Register connection status listener
    MyApplication.getInstance().setConnectivityListener(this);
}
@Override
public void onNetworkConnectionChanged(boolean isConnected){
    setWifiDisplay(isConnected);
}
public void setWifiDisplay(boolean wifi){
    connection = wifi;
    if(wifi){
        wifi_disp.setImageResource(R.drawable.wifi_on);
    }else wifi_disp.setImageResource(R.drawable.wifi_off);
}

Upvotes: 1

Views: 60

Answers (1)

Rahaf Jawish
Rahaf Jawish

Reputation: 46

Well this is not a complete answer, but I think you should read about sendBroadcast(Intent) method that sends broadcasts to receivers, and you should also register your receiver either in activity or in Manifest file.

Upvotes: 1

Related Questions