1

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");
        }
    }
}

}

f3d0r
  • 541
  • 3
  • 12
  • 27
  • 2
    where is dialog.show(); ?? logs are not showing because both conditions are not true put extra else at end and check again. – Dhaval Parmar Feb 11 '16 at 04:13
  • @DhawalSodhaParmar OK, I added an else at the end along with a log, and the log is printing. Could you paste a revised if and else if block that would work for determining whether there is an internet connection or not? – f3d0r Feb 11 '16 at 04:18
  • check one of my answer http://stackoverflow.com/a/15546897/1168654 – Dhaval Parmar Feb 11 '16 at 04:25

2 Answers2

2

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..
        }
user5716019
  • 598
  • 4
  • 17
1

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>
SureshCS50
  • 3,608
  • 1
  • 20
  • 27
  • good catch...but `android:enabled` is true by default and just lets the android system instantiate the receiver. I think you meant `android:exported`, which lets the receiver get events that came from outside the application (which is what OP needs). Adding to my answer. – rothloup Feb 11 '16 at 05:47
  • did you register and unregister your listener in your activity life cycle?? – SureshCS50 Feb 11 '16 at 05:54