I am android beginner and want to declare a broadcastreceiver which reacts on CONNECTIVITY_CHANGE. I have tried the following:
private void checkInternet () {
IntentFilter ifilter = new IntentFilter ("android.net.conn.CONNECTIVITY_CHANGE");
broadcastreceiver = new BroadcastReceiver () {
@Override
public void onReceive (Context Context, Intent Intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService (getApplicationContext (). CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo ();
if (networkInfo! = null && networkInfo.getState () == NetworkInfo.State.CONNECTED)
Toast.makeText (getApplicationContext (), "Internet", Toast.LENGTH_SHORT) .show ();
else
Toast.makeText (getApplicationContext (), "No Internet", Toast.LENGTH_SHORT) .show ();
}
};
this.registerReceiver (broadcastreceiver, ifilter);
}
In Actitvity.onCreate I call checkInternet. When I disconnect the Internet both messages "No Internet" and "Internet" are displayed. And when I connect the Internet both messages "No Internet" and "Internet" come again and in the same order. Can someone please tell me why the Broadcastreceiver 2 times starts and shows 2 different status pro Start? I thank you in advance