Android: Update some weather Data from server every X hours - java

What is the best and most efficient way in android to schedule an update for some weather data from the server every X hours?
Thanks
Sam

If you are asking about scheduling system, a good way is using AlarmManager, which can wake up your activity even if it is not running.
Here is some info: http://developer.android.com/reference/android/app/AlarmManager.html
And here a nice example with Notification: http://android.arnodenhond.com/tutorials/alarm-notification

Register your app with AlarmManager.

Related

Repeating operation in Android without alarm manager

I created application for sending sms interval. I used AlarmManager, which worked good, however after few days of proper working (send SMS at established time). It starts sending at different time or desist.
As far as I know, this is often problem with AlarmManager.
Is there some trap with AM ? or maybe you know better solution, library for that type of problem?
Thanks in advance
if the interval of time for sending the email is more than 15 minutes, I suggest you to use JobScheduler. That work really good over all android versions. I mentioned more than 15 mins because JobScheduler only can schedule at least every 15 mins on android 7 and over.
JobScheduler is an option, but this is only available on Android API Level 21+ (Lollipop v5.0).
A better option is to use WorkManager, currently in beta, that provides the same functionality (and limits like the mentioned 15 minutes minimum interval) but down to API level 14.
You can find more information about WorkManager on this series of blogs and on the documentation.

Is it possible to run a daily task on android without openning the app?

I want to create an app that daily notifies me a value on a web page, I found this answer. It says that JobScheduler will do the trick for the daily part but I am not sure if it is going to run when the app is closed.
By the way I found that if I want to run a job every 3 seconds the code will be the following:
JobInfo.Builder builder = new JobInfo.Builder(1,
new ComponentName(getPackageName(),
JobSchedulerService.class.getName()));
builder.setPeriodic(3000);
If it is daily, should the time be:
builder.setPeriodic(86400000);
Or is there any way to Schedule at a exact time e.g. 9:00 a.m.
Unless your app has been force closed, any job scheduled with job scheduler will run even if the app is terminated. If needed it will be restarted. The only thing that will prevent it is if you declare the job not persistent and reboot the device.
Try to use AlarmManager. You can find example here.

Android - What is the best practice for background tasks?

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.

How to call a function continuously in background in an Android app?

I know it might seem a repeated question, but I want to call a method to report geolocation position every minute as long as the application is open. Other answers recommended using CountDownTimer class. Is there a better way in Android to repeatedly call a function in background?
No idea who suggested a count down timer, but in Android you can use Services namely the Background Service. See here
You could consider something like an AlarmManager - which will run periodically, i.e. every minute to execute an Intent
The risk you run, if something is continuously running, is it will be shut down by the OS.
Alternatively, if you're checking network connectivity etc then you can look into BroadCast receivers - and there's no need to run continuously in the background.
It depends on how you are getting the location. If you use play services you can take advantage of the LocationRequest class. For more information about how to use play services to get the location, please have a look at this training put together by Google.
Hope this helps.

AlarmManager effect on battery for an Alarm clock app

I want to built a alarm clock app for android
I'm using an AlarmManager that starts a service every 1 seconds to check system time.
now I have 3 question:
1- Is there an other way to check system time every 1 second? and what is the best way for this work?
2-what is this my way on battery charge? is it cause to reduce battery charge very faster?
3- what mechanism use the android alarm app for checking time?
please help me to built this app
thanks
Why don't you just use the AlarmManager functionality supplied by the OS, and not try and invent this yourself. Basically this allows you to make a call to a service at a predetermined time (probably ultimately what you want to do).
What you suggest would be very problematic for battery, and doesn't sound like a good approach.

Categories