I am trying to add connection status information to my application. Using the examples here and the google documentation, you will get a receiver that correctly displays a warning when the connection status has changed, but also undesirable displays it whenever an action is created.
ConnectStatusReceiver:
package com.zivtaller.placefinder.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;
public class ConnectStatusReceiver extends BroadcastReceiver {
private String TAG = "netReceiver";
public ConnectStatusReceiver() {
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 (isConnected) {
Log.d(TAG, "Data connected");
Toast.makeText(context, "Data connected", Toast.LENGTH_SHORT)
.show();
} else {
Log.d(TAG, "Data not connected");
Toast.makeText(context, "Data not connected", Toast.LENGTH_SHORT)
.show();
}
}
}
its initialization and registration in the onResume () action:
netReceiver = new ConnectStatusReceiver();
IntentFilter netFilter = new IntentFilter();
netFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(netReceiver, netFilter);
source
share