I am devolping an Android App that need to execute an accumulation of internet tasks periodically (like a web bot). Those tasks need to get stored at a specific time so I thought about using Alarm Manager and a
embeded Database. Due to it, the app could be active much more time, although those save tasks do not need web connection. Later I will throw another Alarm Manager to execute all the tasks queued and do web stuff.
Otherwise I am not sure if it is better to use a foreground service. The app will be working all the day saving the tasks (each 5 or 15 min) but only running task queue with internet each 30mins.
I feel capable of developing both systems but I would like to know which one is better in terms of performance, of battery consumption.
Thank you very much.
Only a recommendation try to find and use a nice library to do it, every Android Update change something about foreground/background servicess.
one is
TimedDog
and for more
enter link description here
Related
I'm software engineer using Flutter in Japan.
Developing an alarm app that wakes me up in the morning, and having trouble implementing the following.
I would appreciate it if you could help me.
Question.
I would like to know if there is a way to execute a specific process at a scheduled time,
even if the app is killed or backGround.
Details.
I'm currently developing on an alarm app that sends local notifications & custom music at a specified time.
I was able to schedule local notifications to send at the specified time.
However, I can't find a way to run a specific process in the background at the same time.
(It's an image of a process that plays specific music selected by the user)
What I've found out
Confirmed that notifications can be sent on a specified day and at a specified time every week using flutter_local_notifications.
(execute flutterLocalNotificationsPlugin#zonedSchedule())
However, in flutter_local_notifications, there is no such thing as a listener to receive the notification itself.
(There is an onSelectNotification, but it doesn't work until user taps the notification bar.)
The library author says that there is no listener to fire such notification timing,
So I decided to use only local notifications in this library and look for another library to replace the background processing.
https://github.com/MaikuB/flutter_local_notifications/issues/1201
I found background_fetch and WorkManager to be particularly useful, but I gave up on using these libraries as well.
The reason is that with the above two libraries can only be executed in the sense of 15 minutes, as shown below, due to iOS limitations,
and I suspect that the process cannot be executed at the intended timing.
workManagaer
https://pub.dev/packages/workmanager#customisation-android-only
// Periodic task registration
Workmanager().registerPeriodicTask(
"2",
"simplePeriodicTask",
// When no frequency is provided the default 15 minutes is set.
// Minimum frequency is 15 min. Android will automatically change your frequency to 15 min if you have configured a lower frequency.
If you have configured a lower frequency. frequency: Duration(hours: 1),
)
background_fetch
Background Fetch is a very simple plugin which will awaken an app in the background about every 15 minutes, providing a short period of background running-time. This plugin will execute your provided callbackFn whenever a background-fetch event occurs.
I think the only way to do this is to use MethodChannel to write and execute native code for each OS from Flutter.
I would like to know if there is a way to execute the scheduled process at the specified time even if the app is in kill or background state in Flutter, even if it uses MethodChannel.
Sorry Its so long. Please let me know.
Thank you very much
The only doc provided for background process (alarm being one of the example cited) points out to an article on medium:
https://docs.flutter.dev/development/packages-and-plugins/background-processes
The whole project consists in making an android application based on crowd sensing that collects data from volunteers (phase 1) and then based on the collected data, every user has a file in the server and the file would be separated into multiple online episodes and I will apply Artificial intelligence algorithms to make the data mining better.
For example, if in phase 1 I can determine that the volunteer is usually at home before 8am and he has no valuable information for me to analyse then the application wouldn't even send data to the server.
I'm still in phase 1, I'm trying to make an android application that starts running as soon as the user in connected to internet, it would send the data (battery, gps, activity...) to the server and based on the activity (walking, running...) it would determine when the next flow of data is sent. For exemple if he is walking then, the next time the data is sent to the server would be in 5 minutes.
While searching, I found many solutions on how to make an android application run in the background while keeping sensors collecting full time (high energy consumption) and the better solution would be to have a schedule for example every 5 minutes.
What I want to know is, is there an android studio solution to make the schedule dynamic? because it depends on the activity as I said before, so each time it collects information it schedules the next time the information is collected again (the next time we use sensors to collect the data then send it to the server).
I hope I made my point clear: what is always running in the background is an application that has a countdown to a specific moment; when that moment arrives the sensors are ON to collect data and send it to the server and that data is also used to make the next countdown (based on the activity data collected) and then the sensor are off until the countdown ends.
I would appreciate your suggestions.
Probably the best option for your project is AlarmManager. It can execute your code periodically or at a particular time, samples could be found here.
Also the app could be launched by system state event(like wi-fi on/off). To catch these events you need to use BroadcastReceiver, samples are here.
So you don't really need to implement countdown timers or background threads. Moreover manually created countdown background thread could be killed by the system at any time.
I have an application that i have developed for ios, android and windows phone 8. The client now requires that the application checks if there is any data to be uploaded to the server every 15-30 minutes and if so, upload it. This is needed because the application is used in areas where there will not be an internet connection. The user adds data into the application and that data needs to be uploaded to the server once an internet connection is available
I have implemented this on android using the AlarmManager to schedule the application to check if there is any data and upload it, and on windows phone i used a PeriodicTask.
Is there such functionality in iOS? When i try googling this, i get many different things appear like NSURLSession, Background Transfer Service and others that say it ain't possible.
So, it is possible and what classes do i need to look into to get this working? If i have to aim at a particular iOS version then that is fine.
You can use NSTimer:
[NSTimer scheduledTimerWithTimeInterval: 1800.0 target:self selector:#selector(yourselector) userInfo:nil repeats: YES];
Apple documentation
No, this is not possible, well may be. Since only specific apps can run in the background like: AudioStreamer, VOIP, Accessory companion or location tracking.
These apps can't really schedule a task. Also it would be really bad for the battery to check some server every 15-30 min.
You can use NSURLSessions to create an uploading task that will run on the background.
You will have to setup the session correctly with all the delegate methods implemented: Background Transfer Considerations.
There are mainly 3 things to consider:
1.) Will the app remain active (visible on the device screen) for 15-20 mins. I suppose that won't be the case.
2.) When the app enters background state (not visible on the app screen, but is in the background), you can perform/schedule tasks for a maximum of 3 minutes.
3.) If the user kills the app (the app is not in background mode) then you cannot do anything.
NSTimer *yourTimer = [NSTimer scheduledTimerWithTimeInterval: 1800.0 target:self selector:#selector(YourMethod) userInfo:nil repeats: YES];
please try this ... this will work...
The upload task supports background operations by default (refer : https://developer.apple.com/reference/foundation/urlsessionuploadtask).
But that is limited to as long as the app is in memory. This is to ensure no app consumes battery without absolute NEED.
What you can do is upload in the background mode and check for pending upload data every time you launch your app.
I want to be reassured that I'm doing this the best practice way :
I have a list which order is changing on the server, therefore, I want every 20-30 sec. to perform a request to see if there were any changes in the order. So, I've created a Service which is bound to the Activity with the ListView, and the service every 20-30 sec. performs a request with an AsyncTask.
I chose to perform it with a Service because I want the list to be updated constantly, even when the application is in the background, And the AsyncTask is because I don't want it to be performed on the main thread.
Is this the right way to do it?
Thank you in advance,
Dan.
It will work, but your app will do a lot of unnecessary work. This will affect battery life. Besides that, according to documentation, if you use device radio, it stays full-powered at least for 20 seconds, which is also no good for battery. You have the following options:
Use Google Cloud Messaging. It will allow you to perform update only when this is really necessary.
If you don't want or cannot use GCM, follow this guide to optimize network access. Start with increasing the update interval (to 4 minutes at least).
I have a repeated task(Task A) to do when I receive a broadcast in my application. I need to know which one is better between these options to do the repeated task?
A Timer in a standalone class and does the Task A repeatedly on timer expiry?
An android background service doing the Task A.
what are the pros and cons in choosing the above methods?
what are the pros and cons in choosing the above methods?
You seem to assume that all possible "Task A" implementations are created equal. You also seem to assume that all periods (one millisecond to one century) are created equal. Neither of these are true.
It is impossible to answer your question in the abstract, and it would take a few dozen pages to explain all the possibilities.
My task need to update the location to my java class variable which is not an activity and has to update to a webserver using HTTPS
If you need to periodically update a Web server with the device location, and the polling period is sensible (e.g., every 30 minutes), your best option is to use AlarmManager and a Service. I wrote a LocationPoller which was designed for this scenario, which another developer has improved upon. Just bear in mind that it may not be possible to determine the device's location at any given moment, so you need to have a "timeout" mechanism in case you cannot find the location, since finding the location keeps the CPU (and GPS radio, where relevant) powered on. LocationPoller has such a "timeout".