0

I would like to build a functionality in my application that when I click on the button the time stoper will count to 5 minutes, but I would like to have it still in progress with exact baner and second when I go to background with application?

  1. How to achieve this what kind of tools I can use? Foreground service, notification and as I understand it is different than widget?

  2. How to start that kind of service with the same custom notification not immadiately but i.e. in next 20 hours or when I trigger my service from backend with somekind of data, then launch this notification and foreground service? Can I use Coroutine work manager for it?

The functionality of this should work exactly like in Spotify. You launch music(in my stuff it is just timer) then when I close my app I have notification that is open and receive data and show for the user. spotify notification

2 Answers2

0

It would be best to start your timer in a Foreground Service so it may not get interrupted whenever your app goes into the background. You do not need a widget for this kind of thing, just a Foreground Service will do the job.

If you want to schedule your timer to run at a specific time, as you said, the best way is to use WorkManager (You can find lots of tutorials if you search for it).

To start your timer from the server, An approach would be using a Silent Push Notification (A notification that does not notify the user, just delivers some data to the app) and starting the timer's service whenever your app receives that specific type of Push Notification.

Amin Mousavi
  • 1,220
  • 10
  • 21
  • What do you mean by that I do not need Widget here? If I have more complicated graphic etc? Should I need widget could you explain what widget is and can I use it in notification, please? It will be quite complicated custom notification with picture like in spotify. Is there a widget not just custom notification? If yes, how to apply widget to notification? Look my edit I share there screen – christianalready Mar 08 '23 at 09:03
  • Widgets and notifications are two different things, Spotify has a widget that users can put inside his/her launcher screen and control the music player, Also it shows a notification so users can control the music player from there too. You can read more about [Widgets](https://developer.android.com/develop/ui/views/appwidgets/overview) and [Notifications](https://developer.android.com/develop/ui/views/notifications) in their own documentation :) – Amin Mousavi Mar 08 '23 at 16:01
0

1 Yes is better to use in such case Foreground service with Notification that will be updated from the service with some period.

2 WorkManager has some time window to run job (battery optimization etc.) If you need exact time we should use schedule alarms operation with Alarm Manager and job that we need. In this case App will be run in the background without any restriction. If your app targets Android 12 or higher, you must obtain the "Alarms & reminders" special app access. Add to the Manifest:

<manifest ...>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<application ...>
    ...
</application>
Djek-Grif
  • 1,391
  • 18
  • 18