Android: register the receiver in the library

I have a library that includes an action with a name BaseActivityand a receiver with a name BaseRegister.

BaseRegisterextends BroadcastReceiverand its actions android.net.conn.CONNECTIVITY_CHANGEand android.net.wifi.WIFI_STATE_CHANGED, and it looks like this:

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

AndroidManifestalso beautiful, according to Log. Here's what it looks like BaseActivity:

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 the project, I already added this project as a library, no problem. And created an action that expands BaseActivity. When CONNECTIVITY_CHANGEas an action, BaseRegisterAndroid starts up, but there is no call for Activity. This is what my project looks like:

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's wrong? Is my approach wrong or is there some kind of error? Any help would be great.

+4
2

BaseActivity , " " , ​​ Java, Android. , BroadcastReceiver , , .

, , Activity , registerReceiver()/unregisterReceiver() onResume()/onPause()

.

+1

BroadcastReceiver AndroidManifest? :

    <receiver android:name=".BaseRegister">
        <intent-filter>
            <action android:name="some" />
            <action android:name="stuff" />
        </intent-filter>
    </receiver>
0

Source: https://habr.com/ru/post/1543169/


All Articles