Reputation: 1543
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?
Upvotes: 1
Views: 4485
Reputation: 23881
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) {
}
}
Upvotes: 1
Reputation: 249
You still have to register it explicitly in onCreate()
or so, with something like
registerReceiver(new NetworkChangeReciever(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
Upvotes: 0