Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Register Receiver in Library

I have a library that includes an activity named as BaseActivity and a receiver named as BaseRegister.

BaseRegister extends BroadcastReceiver and its actions are android.net.conn.CONNECTIVITY_CHANGE and android.net.wifi.WIFI_STATE_CHANGED and it looks like:

public class BaseRegister extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if {
            Log.d("onReceive", "Got it"); // Works
            context.sendBroadcast(new Intent("some"));
        else {
            Log.d("onReceive", "Nope"); // Works
            context.sendBroadcast(new Intent("stuff"));
        }
    }
}

AndroidManifest is fine too, according to Log. Here is how BaseActivity looks like:

public class BaseActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver(applicationControl, new IntentFilter("some"));
        registerReceiver(applicationControl, new IntentFilter("stuff"));
    }

    private BroadcastReceiver applicationControl = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.equals(new Intent("some"))) {
                some();
            } else if (intent.equals(new Intent("stuff"))) {
                stuff();
            }
        }
    };

    public void some() { /** logging etc **/ }
    public void stuff() { /** logging etc **/ }


}

In project, I've already added this project as a library, no problem. And created an Activity which extends BaseActivity. When CONNECTIVITY_CHANGE as an action, BaseRegister triggered by Android, but there is no call for Activity. Here is how my project looks:

public class AnyActivity extends BaseActivity {

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

    @Override
    public void some() {
        super.some();
        Log.d("abc", "abc"); // not working
    }


    @Override
    public void stuff() {
        super.stuff(); 
        Log.d("abc", "abc"); //not working
    }

}

What is wrong? Is my approach improper or does any error exist? Any help would be great.

like image 272
Ogulcan Orhan Avatar asked Nov 10 '22 07:11

Ogulcan Orhan


1 Answers

BaseActivity will invoke the methods "closest" in scope, this is normal core Java behavior and not unique to Android. If you really want to keep the BroadcastReceiver in a base class and signal children then you will need something like a custom listener, i.e. the children register at runtime and when events arrive they will be notified.

Also, is is considered good practice to disable a broadcast receiver when the Activity is not top of stack, so you might wish to use registerReceiver()/unregisterReceiver() within onResume()/onPause()

Good luck.

like image 176
guycole Avatar answered Nov 15 '22 07:11

guycole