Reputation: 561
For some reason, my network change receiver isn't working and broadcasting a CONNECTIVITY_CHANGE to my NetworkStateReceiver class in my Android application. I've checked to see if it is simply a problem with my dialog box, but the Log.d's that are supposed to be printing out aren't.
Here is the code for AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<receiver android:name="com.main.main.NetworkStateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Here is the code for NetworkStateReceiver.java:
package com.main.main;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class NetworkStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle("Connection Failed")
.setMessage("Please Check Your Internet Connection")
.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Code for try again
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
if (intent.getExtras() != null) {
final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
if (ni != null && ni.isConnectedOrConnecting()) {
Log.d("INTERNET_MESSAGE", "Connected to internet");
dialog.dismiss();
} else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
Log.d("INTERNET_MESSAGE", "Disconnected from internet");
}
}
}
}
Upvotes: 1
Views: 843
Reputation: 3768
You need to enable your receiver in manifest.. it will work..
<receiver android:name="com.main.main.NetworkStateReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Upvotes: 1
Reputation: 598
Its better to check Internet Connectivity as follows :
Just make one common function in a Common utility class as
/*
* A Common function to check internet connection.
* */
public static boolean isOnline(Context c) {
try {
ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Now, Use it where you required internet connection in your code as below :
if (isOnline(YourActivity.this)) {
//Your Tasks..
} else {
//Display your AlertBox..
}
Upvotes: 2