I created a broadcast receiver for monitoring my connection data and notify the user. I use an activity with a custom view to show a logo with no connection.
If I register the receiver in the manifest and the app is closed, when change the status of connection, the app is re-opened and I don't want this behavior.
What is the right pattern to follow? Example? Link?
This is my receiver:
public class ConnectionHelper extends BroadcastReceiver {
static final String ACTION_CLOSE_ACTIVITY = "android.net.conn.CLOSE_ACTIVITY";
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (!isConnected) {
Intent mainIntent = new Intent(context, NoConnectionActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent);
} else {
Intent in = new Intent(ACTION_CLOSE_ACTIVITY);
context.sendBroadcast(in);
}
}
....
Thanks in advance for the response.
===UPDATE===
In the end, for my application I used NetworkEvents lib as suggested by @Anderson C Silva.
I created a simple application to help all the others who have doubts about how to solve this problem. github repository