aloj
aloj

Reputation: 3240

Broadcast Receiver doesn't receive after app killed

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?

Upvotes: 5

Views: 3260

Answers (2)

huluyige
huluyige

Reputation: 655

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.

Upvotes: 1

heqingbao
heqingbao

Reputation: 30

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

Upvotes: 0

Related Questions