-2

What are the pro and cons of registering the broadcast receiver via manifest file and also via code?

i registered my receiver via code so that user have the option to start and stop it, somehow, i noticed the receiver is not 'listening' when the app got killed.

It is normal?

thanks.

ML N00b
  • 53
  • 1
  • 1
  • 6

1 Answers1

2

Yes, its normal. You registered broadcast in activity via code, app got killed and broadcast too. If U want your broadcast works, when app doesn't launched, define broadcast in AndroidManifest file.

If U want to your user can "unregister" broadcast, you can add extra logic to your onRecieve function.

When you let to your user "unregister" receiver, just save it in your prefs, or in DB, whatever, and check this value before do work:

@Override public void onReceive(Context context, Intent intent) {
    boolean isUnregisteredByUser = getSharedPreferences("MyPrefs", context.MODE_PRIVATE)
        .getBoolean("IS_UNREGISTERED", false);

    if(!isUnregisteredByUser){
        /* do stuff, handle intent etc */
    }
}

This is easy way, but maybe bad way...

  • thanks Vladymyr, another question is if i registered the broadcast via manifest, would i still be able to give the option to user to unregister it via code? – ML N00b May 20 '16 at 20:40