Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: BroadcastListner Getting error

In my app, I am continuously checking for internet connection. If there is no internet at any point of time, the user gets a toast message.

For this I am making a NetworkStateReceiver class that extends the BroadcastReceiver class.

Java file:

public class NetworkStateReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent); // Here I'm getting the error mentioned below
        Log.d("app", "Network connectivity change");
        if (intent.getExtras() != null) {
            NetworkInfo ni = (NetworkInfo) intent.getExtras().get(
                    ConnectivityManager.EXTRA_NETWORK_INFO);
            if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
                Log.i("app", "Network " + ni.getTypeName() + " connected");
            }
        }
        if (intent.getExtras().getBoolean(
                ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
            Log.d("app", "There's no network connectivity");
        }
    }
}

The error:

Cannot directly invoke the abstract method onReceive(Context, Intent) for the type BroadcastReceiver

This is what I've put in my Android manifest file inside application:

<application>    
    ....
    <receiver android:name=".NetworkStateReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
    ....
</application>

This is the permission I've used:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 

Why am I getting this error in super.onReceive(context, intent);, and how can I get rid of it?

like image 610
Punjan Sudhar Avatar asked Apr 13 '26 22:04

Punjan Sudhar


1 Answers

Remove the line super.onReceive(context, intent). The base implementation does nothing anyway (it's an abstract method).

like image 61
Karakuri Avatar answered Apr 16 '26 12:04

Karakuri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!