0

I want to create a simple service on Android 13 that will do a task after the user unlocks the device. I found different ideas on StackOverflow but most of them target the API 25 or bellow. Since then, we can't declare the BroadcastReceiver in the Manifest anymore and I don't see how I can ensure that my app is launched automatically after the phone is unlocked

This service can definitely be foreground if needed and can be killed once the task is complete to avoid wasting battery but I need something to launch it again when the device is unlocked.

I tried looking at different options including:

  • BroadcastReceiver in the manifest, but it no longer works since API 26
  • BroadcastReceiver in the app, but the app has to be launched once for the BroadcastReceiver to be initialized
  • PeriodicWorkManager with Constraints who looked promising but had nothing related to phone being unlocked or at least phone being booted.

If there is no way to this, a solution that would launch the service after a full stop and restart would also be acceptable.

Thank you for your help!

  • Doesn't the [ACTION_BOOT_COMPLETED](https://developer.android.com/reference/android/content/Intent#ACTION_BOOT_COMPLETED) broadcast still work? According to the "exceptions [list](https://developer.android.com/guide/components/broadcast-exceptions)", that one is -and I quote-: `Exempted because these broadcasts are sent only once, at first boot, and many apps need to receive these broadcasts, such as to schedule jobs and alarms.` I'd say you need to listen to this, and schedule a WorkManager to do whatever is that you need to do in your service. – Martin Marconcini Aug 15 '23 at 10:35
  • You are indeed correct, and it does launch the service after a reboot which was my backup solution. Thank you for pointing this out! This broadcast is however not sent when the phone is just unlocked from a sleeping state – tinyestgrape Aug 15 '23 at 12:58
  • I think in that case you may need to use [ACTION_SCREEN_ON](https://developer.android.com/reference/android/content/Intent#ACTION_SCREEN_ON), however that one is only available via [context.registerReceiver()](https://developer.android.com/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)). – Martin Marconcini Aug 15 '23 at 18:03
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Aug 16 '23 at 14:14

1 Answers1

0

I found a way to do precisely what I wanted: a BroadcastReceiver that listens to "USER_PRESENT". In order to be launched automatically, it has to be declared in the manifest, which is not allowed... except for system apps!

By having the app built into the OS in the right partition, it will gain the permission to declare the receiver in the manifest and everything will work as intended.

For now my problem is fixed, but I don't know how people that can't build their OS can fix the issue.