Android - Service, Boot and check time - android

I have an android app, and I want that the user can decide when (day,hour,and minute) to do something.
So, I have to:
1) start a service when the android phone boots and when the user runs the app (for example after the installation)
2) check (how? how many often?) if the current time is the time choosed by the user
3) run a method if the time is right.
I searched a lot, and I'm a bit confusing...
I found that I have to use a BroadcastReceiver to check when the phone boots,then start an IntentService to have a background process that's check if the time is a "choosen time" and then call the method.
Am I right?
But how do I check the time? How many often should I do that?

You're right. You can use a BroadcastReceiver to check for boot. As for the chosen time, I recommend that you use the AlarmManager class to have your Service run at a particular time or interval.
ref: http://developer.android.com/reference/android/app/AlarmManager.html

Related

How to make android service run every day once

I have a service that runs well but when phone goes to deep sleep , the service stop working .
I want to make use of some android class that makes the service to run everyday
at specific time ,??
i have tried wakelock but it drains the battery very fast for 24/24 cpu on
Any HELP ??
In case this becomes a bigger discussion then our comments. I'll list my answer here.
So your goal is to schedule a job with the alarm manager. Just make yourself a broadcast receiver class and register it on your app's startup. Then you will get code to run on each received notification.
If you need your service to run one time use an IntentService for efficiency instead of regular service.
Also, if you need to make sure it starts automatically then you should register for receiving of phone boot so that you can start your scheduled job again.
The link example:
Schedule a TimerTask at specific time once per day in a service in android
Better solution: you can use any of them alarm manager/jobscheduler/GCMNetworkManager
Create a alarm with looping every 24 hr interval.
That's all you need.
PS: Service will drain your battery.

Proper way to periodically poll location on Android?

I'm looking to build something similar to Google's field trip application. The key attributes that I'm looking to prove out are:
A background "job" that runs every X minutes and checks the user's location and make a webservice call(this question doesn't really concern the location part)
The job should run even if the user exits the main application.
The background job should automatically start up after the device reboots.
After a reboot the application should not show up on the "recent/history" screen
My reading has brought me to a number of different classes/APIs from broadcast reciever of on boot, to regular services, to alarm manager, etc. It seems there are multiple ways this could be made to work, I'm curious what the community recommends as a high level approach?
You would definitely want to use a broadcast manager to get the boot event.
As far as polling for location goes, it sounds like you would want to use a Service and start it once you received the boot event.
Services
You could then use an event bus like Otto to communicate your events to wherever you need.
As far as the timing goes you could use a job manager to run things at various intervals, or just simply create a runnable and have it run as often as you like. As long as you keep it in the service, you should be able to control the length that it will run just fine. Regardless of whether or not the app is closed.
I think you may divide your solution into two parts :
Starting the app or the service after the device re-boot process completed by defining a Broadcast receiver that has the following action :
And on the "onReceive" method of the broadcast "do your task " start the service that listen to the location service updates.
For the part of listening to the location updated every x minutes, you would better use the alarm manager to "wake up" the service every x minutes, and every service sets the alarm for the next call.

Android - Start an IntentService on a specific day/time each week

I'm working on an app that has an IntentService that shows a Notification with showtimes for a local venue. I want the IntentService to be called every Fri at 5am or at boot on Friday morning.
What would be the best way to do this?
Right now I'm thinking of having a parent IntentService that will start at each boot and check what day it is and run the child service accordingly, but I don't know how to deal with a scenario where the user doesn't reboot.
Also, how would I run just the IntentService at boot and not the entire GUI?
Thank you
You can use the AlarmManager and use the setRepeating() method to set an Alarm that goes off every week at the same time.
Your booting requirement is probably not worth the effort, as very very few people reboot their mobile devices often. I've personally gone months without turning my phone off.
However, you don't need a parent service. You can do the checking of the day when the child service first starts up, and have it kill itself if its not Friday.

Android, Is it poosible to use system timer as broadcast receiver?

In my application I need to refresh my data in each 5s or 10s or 1 minute, according to my user preference.
I can create thread and use timer class but i want to know is there any other way? for example timer of system sends flag (broadcast something) and i get it in application (in order to refresh my data)?
Thanks
If you want to refresh the data every serval seconds, you can setup a Hanlder to do this. However, this requires your activity keeps running.
If you want to do something at a specific time, like an Alarm. You can use the AlarmManager. This would lauch your activity or other components even it is not running now.
If you just want to moniter the change in the system time. Well, there is a Intent ACTION_TIME_TICK . You can register a BroadcastReceiver to listent to it and implement your own code when the systemt brocasts it. However, the "TICK" interval is "One Minute" only and can not be changed.
AlarmManager is what you are looking for. It can be used to set up periodic events that are delivered via a PendingIntent (which can be turned into an Intent broadcast).
I would be wary checking for new data on such a regular interval unless the device is always connected to a power source, especially if you are making the check over a network. With that small a poll interval going to the nework the battery will be flat in hours.

How can I ensure a service is started at 9am and stopped 9pm every day?

I have a service that will monitor location changes daily. What I know so far that to start a service at boot, I have to follow the linked tutorial. This way I can get the service started at boot, but to save battery I need it only between 9am-9pm.
Question is pretty simple, so I will repeat:
How can I ensure a service is started at 9am and stopped 9pm every day?
Use AlarmManager to set two alarms, each with a PendingIntent that will call startService() on your service, but with distinct action strings ('start', 'stop'). When onStart() of your service detects the 'stop' action Intent, it arranges for an orderly shutdown (e.g., stopSelf()).
This will fail if the user applies a task manager to you in Android 2.1 or earlier, since the technique they tend to use will wipe out your alarms (in addition to killing the service). In that case, the user is presumably voting for your service to not run, so you should try to accommodate the user's wishes.
CommonsWare is right. This is the best way.
You are writing an application and you had better not to change anything out of the application. If you want to add a system service(on boot started service), you need to modify the BSP and add your service to systemserver.java. It's not recommended.CommonsWare's suggestion can do this work.
As you said about the activity, you can start the activicy when you receive the boot broadcast. Then do what your want.....

Categories

Resources