1

I have a class that checks whether or not the Internet is connected while my application runs:

public class NetworkChangeReceiver extends BroadcastReceiver { @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){ Toast.makeText(context, "CONNECTED!", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(context, "NOT CONNECTED", Toast.LENGTH_LONG).show(); } }} 

I added this receiver within my manifest file, in between the application brackets

 <receiver android:name=".DataHelpers.NetworkChangeReceiver"//DataHelpers is the package name android:label="NetworkChangeReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> </intent-filter> </receiver> 

When I run my app, which relies on the internet, and shut off the connection, shouldn't I be getting a Toast message that says "Connected!"? Shouldn't the receiver recognise that there is no connection and fire off the onReceive() method in my NetworkChangerReceiver class?

3
  • did u register your receiver on activity's onResume? Commented Dec 27, 2016 at 14:45
  • No, how do I do that? Could you write an answer? I thought registering just meant including it within the manifest file? Commented Dec 27, 2016 at 14:46
  • do you ask for permissions? Commented Sep 18, 2018 at 6:08

2 Answers 2

1

You can try this:

 public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 2; public static int TYPE_NOT_CONNECTED = 0; public static int getConnectivityStatus(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return TYPE_WIFI; if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) return TYPE_MOBILE; } return TYPE_NOT_CONNECTED; } public static String getConnectivityStatusString(Context context) { int conn = NetworkUtil.getConnectivityStatus(context); String status = null; if (conn == NetworkUtil.TYPE_WIFI) { status = "Wifi enabled"; } else if (conn == NetworkUtil.TYPE_MOBILE) { status = "Mobile data enabled"; } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) { status = "Not connected to Internet"; } return status; } } 

define the BroadcastReceiver:

 public class NetworkChangeReceiver extends BroadcastReceiver { public boolean isConnected = true; String status; Context Cnt; Activity activity; Activity parent; AlertDialog alert; public NetworkChangeReceiver(Activity a) { // TODO Auto-generated constructor stub parent = a; } @Override public void onReceive(final Context context, final Intent intent) { activity = (Activity) context; status = NetworkUtil.getConnectivityStatusString(context); if (status.equals("Not connected to Internet")) { //Toast.makeText(context, "Internet connection required", Toast.LENGTH_LONG).show(); } ReturnStatus(status, context); } public void ReturnStatus(String s, final Context cnt) { if (s.equals("Mobile data enabled")) { isConnected = true; //Intent intent = new Intent(activity,activity.getClass()); //activity.startActivity(intent); } else if (s.equals("Wifi enabled")) { isConnected = true; } else { isConnected = false; final AlertDialog.Builder builder = new AlertDialog.Builder(cnt); // Set the Alert Dialog Message builder.setMessage("Internet connection required") .setCancelable(false) .setPositiveButton("continue", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { activity.finish(); Intent intent = new Intent(activity, activity.getClass()); activity.startActivity(intent); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { if(alert.isShowing()) { isConnected=false; alert.dismiss(); } } }); alert = builder.create(); alert.show(); } } public boolean is_connected() { return isConnected; } } 

Now use them in any activity :

public NetworkChangeReceiver receiver; Boolean bl = true; public void checkInternet() { IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); receiver = new NetworkChangeReceiver(this); registerReceiver(receiver, filter); bl = receiver.is_connected(); Log.d("Boolean ", bl.toString()); } 

Unregister the receiver in onPause()

 @Override protected void onPause() { super.onPause(); try { unregisterReceiver(receiver); } catch (Exception e) { } } 
Sign up to request clarification or add additional context in comments.

Comments

0

You still have to register it explicitly in onCreate() or so, with something like

registerReceiver(new NetworkChangeReciever(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); 

2 Comments

Do I have to do this for every activity or is there a general way to have it run through the entire application?
you can do it generally too, by registering it in the Application class. Here is some link with more info stackoverflow.com/a/12834521/1967672

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.