Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 848
aloj Avatar asked Nov 11 '22 13:11

aloj


1 Answers

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.

like image 132
huluyige Avatar answered Nov 14 '22 23:11

huluyige