5

I have created a broadcast receiver to manage these events (ACTION_SCREEN_ON, ACTION_SCREEN_OFF and ACTION_USER_PRESENT). I register my broadcast receiver like this in my main activity

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);

mReceiver = new UnlockReceiver();


registerReceiver(mReceiver, filter);

My problem is that the UnlockReceiver doesn't receive after my app is killed (when I swype it from app selector). I have this problem because these broadcast can't be declared on Manifest I've tried solve it using Service and AlarmManager. How can I solve it?

Kara
  • 6,115
  • 16
  • 50
  • 57
aloj
  • 3,194
  • 7
  • 27
  • 38
  • Refer Commons Ware's answer for http://stackoverflow.com/questions/10398053/android-stop-broadcast-receiver-being-killed-with-service – coderplus Feb 08 '14 at 16:34
  • 1
    "My problem is that the UnlockReceiver doesn't receive after my app is killed (when I swype it from app selector)" -- that is because your process is terminated, and so your receivers no longer exist. "How can I solve it?" -- what is there to solve? By swiping your app off the recent-tasks list, the user has indicated that the user does not want your app to be running. – CommonsWare Feb 08 '14 at 16:47
  • I want to execute my receiver always when SCREEN_OF, SCREEN_ON or USER_ACTION happend, like if these broacast were declared in Manefiest – aloj Feb 08 '14 at 16:52
  • Finally I've solve the problem. I have declared one broadcast receiver for ACTION_USER_PRESENT in Manifest (this one can be declared there, I didn't know it) and when this Broadcast receiver is execute I register the other broadcast receiver for ACTION_SCREEN_OFF. I have deleted ACTION_SCREEN_ON, I don't need it – aloj Feb 08 '14 at 18:19

2 Answers2

1

you dynamically register the broadcast receiver in the activity, that's why it doesn't work.

Declaring it statically in manifest will work, but that's not your case.

I have a workground, and it's up to you to decide to use it or not:

Use a sticky service that works always in background, and register your broadcast receiver in the service.

huluyige
  • 645
  • 4
  • 10
0

Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.

Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

The platform defines two new intent flags that let a sender specify whether the Intent should be allowed to activate components in stopped application.

FLAG_INCLUDE_STOPPED_PACKAGES — Include intent filters of stopped applications in the list of potential targets to resolve against. FLAG_EXCLUDE_STOPPED_PACKAGES — Exclude intent filters of stopped applications from the list of potential targets. When neither or both of these flags is defined in an intent, the default behavior is to include filters of stopped applications in the list of potential targets.

https://developer.android.com/about/versions/android-3.1.html

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
heqingbao
  • 30
  • 4