Problem with PeriodicWorkRequests using WorkManager, in android - java

I have implemented periodic work request to update the weather and show the notification after a fixed period of time. But, it is not working properly. One day the notifications are shown after fixed intervals but another day the notifications do not appear at all unless I open the app.
Can anybody explain the possible reason for this bug?

Related

Alarm set by alarm manager is cleaned after app is closed

I have been roaming in forums for days and all I found was same repeated responses which don't work at all.
I am trying to set an alarm for a certain time (e.g. 8PM) to send a notification. However, as soon as I force close the app the alarm is also cleared. It works fine if app is running in the foreground or minimized.
Peopled asked this in many other questions and received same questions that none of them work. I brought some of them in the following:
use service(sticky or other types)
use flag FLAG_INCLUDE_STOPPED_PACKAGES for the intent
start service again in onDestroy or onTaskRemoved
some other responses none of which seems to work.
Do you know any best practice to set some alarms to send a notification at a certain time?
it seems like this problems happens in some devices with restrictions like MIUIs.
Thanks in advance for your responses.

Difference between Service and PeriodicWorkRequest()

I have to work on a project Where I have to upload users' locations every 15 minutes. For that, I searched a lot and found Recurring work with PeriodicWorkRequest. But the problem is that the WorkManager might not work when the app is closed/killed per the answer given here. Then I found about Service in android.
So I want to know If I want to send users' locations every 15 min even when the app is killed then how to approach this?
If an application is Force Stopped, the OS cancel all the Job related to that application. This is not a WorkManager only problem. The OS interprets a Force Stop as an user request to the OS that they don't want this application to run anymore.
Even if you use JobScheduler or a Service, the application is gone. But a force stop should be a user decision.
Some OEMs have implemented in the past some changes to the Android OS so that a swipe out of an application from the launcher was interpreted as a force stop with all the negative effects on scheduled jobs. This is where the problems start.
WorkManager is this case has implemented some mitigation, but the application cannot do anything if it is force stopped till the user launch it again.
If have a problem with a specific OEMs, please open an issue on the Android issuetracker as this maybe a CDD violation. Google can contact the OEM and request that they fix the ROM. This is going to take time, in the meanwhile, you can take a look at sites like don't kill my app to understand what are the constraints on a specific device and use a library like autostarter to help the user to navigate to the right setting.

How to create a reliable repeating task?

I'm really new to Android (and java) programming. I'm struggling to create a reliable repeating background task. It should run quite often, let's say in every 10sec, even if the phone is in sleep. I've tried already a lot of things, but without success.
As I learned, AlarmManager is not reliable under ~15min interval.
Tried with FireBase JobDispatcher also.
Now I'm using service and timertask with scheduleAtFixedRate in the background. Till now this is the best, but still unreliable.
In every run of the task I have to do a database read, do some notification if needed, and write some data to DB.
I'm using FULL_WAKE_LOCK also, but still have a lot of missing notifications, or sometimes I get 10-20 notification in the same time with the delay of few minutes.
What would be the right way to do this job and make it reliable?
(push is not possible at the moment from server side)

Multiple Notifications from Foreground service Android

I'm working on an application where I need to show notifications 5 times a day ...from a foreground service..the purpose of using the foreground services is to be able to display notification even if the
application is closed . so I want to start the service as applications starts and schedule its notification timing accordingly I have tried using the snippet provided on android developers forum but its not working for me ... any help and sample code is highly appreciated Thanks
If the notifications are to be fired at a fixed time, you may not need a foreground service. It is a bad idea to keep a service running just to show notifications. Schedule those five times using an AlarmManager. You can find tutorial for it in google.
You would need to schedule alarms one by one. Foe ex..when alarm manager wakes your app the first time , then schedule a second alarm and son on.. You may not be able to set 5 concurrent alarm schedules. Do it one by one..

Remove reliance on the Android clock entirely and use custom time from server

I have an application which records information using a tablet which is sync'ed to a server where reports are then run on the results. The users spend all day using the tablets and travel a lot throughout the day.
The time when the users complete these tasks is crucial to the reports and we have had several occurrences where we can't rely on the device time.
The user sometimes manually sets the time to something different on the device in order to look like they have done more work - this happens more than we expected
When relying on the time set to 'automatic', the time sometimes jumps around as the users travel between networks
When the tablet is initially switched on, sometimes the device time is set to 1970
I have a webservice to get the server time which is not currently used because we can't rely on the users having internet connection because they generally don't have any connection at all due to the nature of the work. However, they sync the data everyday usually, or at least once every few days.
I'd like to create something that gets the server time and counts the time from then on and the app would use this time. Any time they connect, this time will be updated, just to make certain the time is synced. Does anyone have any suggestions as to the best way to do this? Bear in mind that the time that we send up is crucial - it could be the case that if misreported on several occasions, someone could lose their job.
Thanks in advance!
Android do have 3 different kind of timer...
SystemClock
One of them will be useful to you,uptimeMillis()
Once the app starts, save the current uptimeMillis... when the app ends.. you could get the difference from "uptimeMillis() - oldUpTimeMillisThatWasStored"...
Android do sent a broadcast when the time or timezone is changed...
And if you have a broadcast receiver which catches for these changes from manifest file...
you will know when the time is changed (even if you app is not running)..
ACTION_TIME_CHANGED
You need persistent internet connection. Otherwise you have to rely on client time which can be modified as you already mentioned. You may be able to track the changes, but only as long as the app is running which can easily be faked by disabling the app (Settings -> Apps) before changing the time.
Scenario:
User completes task, client time gets stored
User modifies time
Device gets internet and syncs with server, telling him its current time and the task so server can calculate tasks "real" completion time. <-- ERROR
I left this unfinished, but have now implemented a solution.
I've created a Service which runs in the background, and downloads the date at intervals from a webservice, and uses a Timer to increment the time in between calls to the webservice.
Further to this, I use GPS to get the current time and date, and it uses this if possible, and if not then it uses this internal clock.
The time should be right most of the time, in theory!

Categories

Resources