I am new to Android and working on this chat app where I want to change an Activity element based on a predetermined data on predetermined exact times. It doesn't have to work when app or phone is closed but I want to send notifications as well with the change. There is AlarmManager, JobScheduler, Handler, Timer, WorkManager but I am extremely confused about how they compare to each other. Which is the best option in this case?
In fact, WorkManager under the hood uses all of these scheduling APIs depending on the API level. This means that you won't have to deal with finding the best API for devices, because WorkManager will pick up the appropriate API for you.
You can also read the official documentation for more info and comparison: https://developer.android.com/topic/libraries/architecture/workmanager
The WorkManager API is a suitable and recommended replacement for all
previous Android background scheduling APIs, including
FirebaseJobDispatcher, GcmNetworkManager, and Job Scheduler.
Related
I am new to Android. I want to develop an app that is going to do something every 20 minutes: if the user is in the app, they just get a message, else, the app will set a dialog and if the user accepts that dialog, the app will open and the user will get that message.
I have searched how to do that and ended up using alarm manager and everything went fine. However, the question is that if using alarm manager is good for this situation. If not, why? And what is the solution? I had read somewhere that work manager is also good.
WorkManager will not be useful in a case like this when device enters Doze mode.
WorkManager adheres to power-saving features and best practices like Doze mode
I have seen that even after white listing the app, (removing from battery optimisation), if device is left unused, stationary, unplugged, WorkManager work is delayed until the next maintenance window, sometimes hours of delays.
AlarmManager can be used but documentation recommends
Exact alarms should only be used for user-facing features. Learn more about the acceptable use cases for setting an exact alarm.
FCM is another option that could be considered in doze mode.
Edit: WorkManager is definitely recommended for persistent onetime or periodic works which are not time sensitive, where combination of constraints can be applied.
This question already has answers here:
How to schedule notifications using WorkManager?
(5 answers)
Closed 4 years ago.
I already used Android Job library for schedule job but Once Google announced Workmanager library in I/O 2018 . I want to use it as alternative but unfortunately I couldn't use it to schedule notification at specific time like - for example - event notification at 8:30 am
This is my workmanager request code
Data data = new Data.Builder()
.putString("EVENT_ID", event.id)
.putString("EVENT_TITLE", event.eventTitle)
.putString("START_TIME", event.startTime)
.build();
long delayToPass = getTriggerTime(triggerTime);
OneTimeWorkRequest compressionWork = new OneTimeWorkRequest.Builder(EventWorker.class)
.setInputData(data)
.setPeriodStartTime(delayToPass, TimeUnit.MILLISECONDS )
.build();
WorkManager.getInstance().enqueue(compressionWork);
my code read trigger time from database .. I want to run notification at trigger time in database when Event occur via Workmanager library
WorkManager is not designed for that scenario. You would have to use AlarmManager.
Thanks, but I already use Evernote Android Job library to achive this scenario . but I read this next quote in their github readme
WorkManager is a new architecture component from Google and tries to solve a very similar problem this library tries to solve: implementing background jobs only once for all Android versions. The API is very similar to this library, but provides more features like chaining work items and it runs its own executor.
If you start a new project, you should be using WorkManager instead of this library. You should also start migrating your code from this library to WorkManager. At some point in the future this library will be deprecated.
Starting with version 1.3.0 this library will use the WorkManager internally for scheduling jobs. That should ease the transition to the new architecture component
I want to know what it means?
my app lets user create scheduled task.
Example: Tomorow show this notification => this notification leads to this activity etc.
What is the best practice to do this? Do I need to use some background task manager? How can I do this so it will work even when App is not running?
Facebook is doing something similar, when it is not running it still checks web for useful info for you.
Can somebody point me to some tutorial or get me some example? Thank you
There are several solutions that are recommended by Android framework team
Firebase JobDispatcher
GCM Network Manager, codelab
Job Scheduler api 21+
For showing a notification at a known point in the future (either a specific time or X hours from now) use AlarmManager to set an alarm.
For getting notifications from a server, generally you'd use push messages from the server. Although polling the server on an alarm would also work.
I would want to constantly check for new SMS from a background service (which suns even if application is stopped).
I wonder if Codename One supports background services as Android platform supports https://github.com/codepath/android_guides/wiki/Starting-Background-Services.
Codename One supports services using native interfaces on Android. Other platforms don't have anything quite like Android's services so this isn't something that can be generalized at that level.
iOS/Windows Phone do not allow intercepting SMS's and Android itself has made significant changes to incoming SMS logic which restrict this functionality significantly so either way this isn't something you should do. Generally the recommended way to handle incoming wakeup is to use push notification.
All I’m really looking for here is a little guidance. I’m trying to create my first official android app. I’m new to java/droid but not programming. Over the past month I’ve created quite a number of little experimental activities, services, threads and whatnot and they all function as planned. So now I’m trying to tie is all together but not having much luck.
In a new project I’ve compiled the guts into 'my.main.package' which runs a service that is constantly crunching data that other clients/apps can use… Well that’s my plan. For example, in this service is a custom thread/loop timer that is constantly counting. What would be the best way for any other apps to get a constant feed of this timer and other data as a listener could within its own sandbox and in the least taxing way possible?
I’m assuming one must implement aidl for IPC but I’m not sure if its needed and/or necessary as data from my.main.package is only outgoing, i.e. other apps only need receive/listen. I understand there needs to be some form of message handling or parcelable marshalling going on and possible permissions with aidl but I got to thinking that encoding/decoding a parcel or sending a message every millisecond would be very taxing. Is aidl the only way to go or is there a way to broadcast data as you can intent?
Any help would be greatly appreciated!
In a new project I’ve compiled the guts into 'my.main.package' which runs a service that is constantly crunching data that other clients/apps can use
A service should only be running when it is actively delivering value to the user. Users think that developers who create services that run all of the time are idiots and attack their apps with task killers, force-stops from the Settings app, etc.
Perhaps your description is just depicting your app in a poor light, but "a service that is constantly crunching data" is an anti-pattern. These are phones and tablets, not servers.
What would be the best way for any other apps to get a constant feed of this timer and other data as a listener could within its own sandbox and in the least taxing way possible?
The best way is for them not to be separate apps in the first place.
For example, in this service is a custom thread/loop timer that is constantly counting.
This is not adding value to the user.
I’m assuming one must implement aidl for IPC but I’m not sure if its needed and/or necessary as data from my.main.package is only outgoing, i.e. other apps only need receive/listen.
A remote service using AIDL is one way of doing IPC. It is not the only way. It is not even the most common way. You can also:
send a broadcast Intent
have the client send a Messenger to the service, and the service sends messages to the client via that Messenger
have the service update a ContentProvider, and have clients register a ContentObserver on the ContentProvider
I understand there needs to be some form of message handling or parcelable marshalling going on and possible permissions with aidl but I got to thinking that encoding/decoding a parcel or sending a message every millisecond would be very taxing.
IPC is "very taxing" in general. Hence, IPC should be avoided wherever possible.
In fact, IPC with AIDL is the only & the most effective way to do your requirements, I think. If you need one demo, you can take a look here. It's a simple example I wrote to learn AIDL & Binder on Android. Hope it could give you some brief for starting.