简体   繁体   中英

Android: Internet BroadcastReceiver for the application

In my application I need not to allow the user to use the app if there is no internet connection. I know there are a bunch of good tutorials out there to do this, but non has actually fulfilled the requirements I need.

The internet connection should be checked all time in the app. That's why, and based on this part of the documentation, I registered my receiver in the Application class. However, the problem was where to unregister the receiver , since there are no onDestort(), onStop() methods in the class.

The other option I tried is to (un)register the receiver in my BaseActivity class which extends AppCompatActivity and all other activities in the app extends the BaseActivity , but I keep getting java.lang.IllegalArgumentException: Receiver not registered

Lots of questions were solved by unregistering the receiver in onStop() if it was registered in onStart() , or to unregister it in onDestroy() if it was registered in onCreate() , but that didn't work for me as well.

public class ConnectivityReceiver extends BroadcastReceiver {

    public interface ConnectivityChangedListener {
        void onConnectivityChanged(boolean isConnected);
    }

    public static ConnectivityChangedListener listener;

    @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 (listener != null) {
            Log.i("Connectivity", "" + isConnected);
            listener.onConnectivityChanged(isConnected);
        }
    }
}

My BaseActivity.java (Other activities extend this one)

public class BaseActivity extends AppCompatActivity implements ConnectivityReceiver.ConnectivityChangedListener {

    private static BroadcastReceiver br;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        br = new ConnectivityReceiver();
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        ConnectivityReceiver.listener = this;

        registerReceiver(br, filter);

    }

    @Override
    public void onConnectivityChanged(boolean isConnected) {
        Log.i("Connectivity", "Activity " + isConnected);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(br);
    }
}

However, the problem was where to unregister the receiver, since there are no onDestort(), onStop() methods in the class.

  1. You can create a service and create an inner broadcast receiver class that will do the above work. In the onDestroy() method of the service you can unregister the receiver. Register in the onCreate() method of the service.

The advantage of this is that you can perform some long operation because a broadcast receiver will just execute the code inside onReceive() and exit away, say if you want to start any service or do other work that takes time then putting inside service is the best.

I guess other than that there is no place you could put it elsewhere.

  1. Create a static method in broadcast receiver in a Application class like this,

    static ConnectionReceiver mConnectionReceiver; static Context appContext; static void registerConReceiver() { mConnectionReceiver = new ConnectionReceiver(); IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"); appContext.registerReceiver(mConnectionReceiver,filter); }

Then unregister whenever you want

static void unregisterConReceiver()
    {
        try{
             appContext.unregisterReceiver(mConnectionReceiver);
            mConnectionReceiver=null;
        }catch (IllegalArgumentException e) {
            mConnectionReceiver = null;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM