Activity, service or fragment? - android

I am writing an app which needs to monitor the current battery level, after every 15 minutes or so, continuously.
To try and make it relatively efficient, currently, I am using AlarmManager.RTC, with PowerManager.PARTIAL_WAKE_LOCK, and an setInexactRepeating of 15 minutes.
This will be a sort of battery drain % monitor app, so I need this app to be always logging what it finds every 15 minutes, even after boot. It checks for battery level, if the phone is charging and some other things such as if wifi is enabled, if screen is on etc. and then writes these values in sqllite for later analysis.
So which one would be the best way to go about it:
AlarmManager on an activity in a BroadcastReceiver in the
activity
AlarmManager and a service
Headless fragment
Or a better option is suited?

service - for background work, receiver for device boot event(where you should start service), and activity for showing results.. and Timer for timer taks?
do you realy need AlarmManager for this task? )

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.

for aggressively repeated alarms, which is better Alarm or Service with Timer task?

I am trying to do an application that shows the user notification with options that user can choose:
Aggressively repeated: like every 3 minutes (does not wake-lock the device)
Highly repeated: every 10 to 15 minutes (does not wake-lock the device)
Repeated: every 1 hour
once a day
so for the first 3 options, (as a Performance and battery life) is it good solution to set up Timer and start a scheduleTimerTask within Service ? or should i set an Alarm from AlarmManager?
Service
Constantly runs in Background , will drain battery
Can be killed by android or user might go to setting and kill it manually
Will be killed if phone is switched off
service was basically meant to do background services like loading and
uploading data on separate thread, for your purpose AlarmManager is
useful and it will not be killed by android and probably will drain
less juice than service
It's better to use an alarm. A service can be killed by the OS.

TimerTask vs AlaramManager, Which one should I use?

I am working on an application which triggers an action (say toast message) every 10 minutes after the screen is ON and stops the action after the screen is OFF.
I have used TimerTask for this purpose.
Shall I start using AlaramManager instead of TimerTask or shall I keep using TimerTask ?
I know the difference between the two but can't figure out which to use.
Cant' agree with the nikis' answer
Timer and AlarmManager are solutions addressed to satisfy different needs.
Timer is still a "task" that means this is a thread of your application that means that some component of your application must be running on device to keep timer alive.
If you set timer for 10 minutes events - you can't be sure if your application will not be disposed by system in some moment. If device will be turned into the sleep mode your timer can be stopped. To prevent behavior like that you have to use PowerLock's and drain battery
AlarmManager is system service (runs outside your application) that means that the pending intent will be sent even if your application is killed after setting the alarm.
Some examples:
You have to blink some "led" on the view every 1 s - use Timer - you need it only when application is in foreground, there are short intervals - no point in using AlarmManager for task like that.
You have run some task once after 10 s - Handler.postDelay(); will be the best solution for that, and the job will be done on main thread (UI).
You have to check every 10 minutes if there is some new content on device that you are supposed to push to the server - use AlarmManager - your application does not need to be alive all the time, just let system to start job you want every 10 minutes - that's all.
In most cases you should definitely use AlarmManager, because (from the docs):
The AlarmManager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the AlarmManager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes.
Although you don't need to fire any event while screen is off, AlarmManager still saves the battery by grouping alarms, when you use setInexactRepeating (but this is not important for you, because your interval is 10 minutes). And moreover, it can fire an event is app is not running. I vote for AlarmManager, because it's good practice, but considering your conditions, you can leave Timertask.
BTW, you can also use Handler, which I believe will be the best choice.

How to optimize android app for battery usage

I created an application which runs on background in every 3 minutes and do some calculation and update data on sqlite database.my app is consuming more battery after installing this app.
so can you please update me how to create app for optimization battery life.
Thanks.
Yes offcourse it is possible, your application is draining battery because you are running your service all the time but instead of that
....................................................................................................................
The best practice is to
create a AlarmManager context using PendingIntent, Now for every 3 min set an alarm.
create a BroadcastReceiver which receives alarm after every 3 minutes.
NOW....
from that Receiver start your service do calculations on that service and save it into database. after saving clear() all the data of intents, database objects and also close the database connections.
and after doing all these things call stopService() to stop your service immediately.
This makes your application to startService() only after every 3 mins and after doing all calculations it stops service.
check with this and i am sure you will not get any problem regarding battery because your service will run for max 2 secs on every 3 mins :)

Do I need an Alarm or a Service to perform a repetative task?

I am developing a gps tracking for Android i want my application to start when the phone boots up and send gps coordonates from 10 in 10 minutes.
Do i need to use a service or an AlarmManager broadcast receiver can do the job ?
Prefer the AlarmManager over the service if your frequency of getting the geo co-ordinates is much lesser say 1 location in few miutes...
Otherwise if your frequency is much higher say for every 20/30 seconds then prefer service because AlarmManager is quite heavy in the case of few seconds to remind...
You can start service on phone boot and listen for coordinates changes. See this article
Start service at boot

Categories

Resources