Android- Automatically change Home or Lock screen wallpaper whenever screen gets unlocked or locked
- Change home screen wallpaper whenever screen gets unlocked: Done
- Change lock screen wallpaper whenever screen gets locked: Todo
- Settings screen to coose for event (screen lock/unlock): Pending
- Asking
WRITE_EXTERNAL_STORAGE
permission on app launch: Pending - Interface where user can add wallpapers: Pending
- Interface to enable/disable this app: Pending
A Service is an application component that can perform long-running operations in the background, and it doesn't provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application.
Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern. These broadcasts are sent when an event of interest occurs. For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging. Apps can also send custom broadcasts.
With WorkManager
, developers can easily set up a task and hand it off to the system to run under the specified conditions.
A task is defined using the Worker
class. The doWork()
method is run synchronously on a background thread provided by WorkManager.
While a Worker
defines the unit of work, a WorkRequest
defines how and when work should be run. Tasks may be one-off or periodic.
- App always keeps running a Foreground Service
- This service registers a Broadcast Receiver
- This broadcast receiver listens for phone screen lock & unlock events
- Whenever screen is unlocked, receiver calls a method
setRandomWallpaper
- This method picks a random wallpaper from
dynamic-wallpaper
directory (inside phone storage) - If this directory doesn't exists then it creates one
- User must manually add some wallpapers to this directory (until UI is built for it - Todo)
- User must explicitly grant
WRITE_EXTERNAL_STORAGE
to this app (until app starts asking for it automatically on launch - Todo) - Depending upon the devices, User might have to explicitly grant AutoStart permission to the app
- Whenever Service (
MyService.java
) is destroyed, it sends broadcast intent to a Receiver (MyReceiver.java
) fromonDestroy
lifecycle method - This custom Broadcast Receiver starts the Service again
onDestroy
method of Service (MyService.java
) is not always guaranteed to be called and hence it might not get started again- To overcome this limitation, app schedules a
PeriodicWorkRequest
viaWorkManager
- This Background Job runs on every 16 mins and it's job is to restart the Service if not already running
- This Job is also responsible for launching Service for the very first time
- Android system guarantees that this Background Job will run given that it has AutoStart permission (even if device restarts)