1

I am creating widget app that sync data with server i am registering BroadCastReceiver dynamically for receiving SCREEN_ON and SCREEN_OFF broadcasts i registered my BroadCastReceiver in class that extends Application, but the problem is that if the process is running it app receive SCREEN_ON and SCREEN_OFF broadcasts but if process is died then application would not be able to get receive broadcast why? in BroadCastReceiver theory they says app will receive broadcast even if it is not running.

public class ThisApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Utils.logCat("ThisApplication", "onCreate()");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        registerReceiver(new ScreenOnOffReceiver(), intentFilter);
    }
}

public class ScreenOnOffReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
             Utils.logCat("Screen ScreenOnOffReceiver", "SCREEN is ON");
        }
        else
        {
             Utils.logCat("Screen ScreenOnOffReceiver", "SCREEN is OFF");     
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Shan Ali
  • 21
  • 3
  • Try to register broadcast receiver from service – Rajesh Jadav Aug 18 '15 at 08:00
  • @MitchDart the Receiver for Receiveing SCREEN_ON SCREEN_OFF broadcase should be registerd dynamically as the docs says [link]http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_ON – Shan Ali Aug 18 '15 at 08:23
  • @RajeshJadav i am doing this right now thanks i will give you feedback when this solves a problem or not – Shan Ali Aug 18 '15 at 08:25

2 Answers2

0

1 - You forgot to add the context to your new ScreenOnOffReceiver() Change it to new ScreenOnOffReceiver(this).

2 - I guess you have an onPause(); method containing unregisterReceiver()?

This makes sure that when your app closes the BroadcastReceiver gets closed or unregistered as well. So it would be better to register your receiver directly using the manifest file. If for some reason you do HAVE to register it dynamically: Use an if statement to check if your receiver is listening. If it is don't create another one. If it is not create one. If you want to know how(the code) comment below and I will do so)

KISHORE_ZE
  • 1,466
  • 3
  • 16
  • 26
  • @Shan Ali It will be useful if you post your full code. Or atleast if you have an onPause() / onResume() methods – KISHORE_ZE Aug 18 '15 at 08:05
  • i dont implement onPause/onResume because a i am doing work in Application context these methods are not available there – Shan Ali Aug 18 '15 at 08:28
  • @ShanAli could you please either post your source code or atleast say why you do not wish to register your receiver directly in the Manifest.? – KISHORE_ZE Aug 18 '15 at 13:33
0
  • BroadcastReceiver registered in the AndroidManifest.xml will receive broadcasts even if the process is not running
  • BroadcastReceiver registered during runtime only exist while the VM is running and will not be called if the process dies.

That is the rule, that is the indented behavior, it is always been like that, you cannot change it.

Said that:

if you want a you can during runtime enable/disable a BroadcastReceiver that is registered in the manifest like in this answer:

Enable and disable a Broadcast Receiver

Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144