How to optimize android app for battery usage - android

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 :)

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.

How do I make a thread in a service that's guaranteed to run every given specified time?

I need to run some queries every 24 hours, I have a service that's run when the app is launched and the service stays as wanted, How can I have a thread running in that service that inserts some data into the sqlite database?
You can't assure a service stays around like that. Android can kill a service at any time. Instead, use JobScheduler to schedule a job to do the inserts in. Understand that it will not be an exact alarm and will be subject to battery saving rules like doze mode. Unless your app is whitelisted from those, of course.

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.

Activity, service or fragment?

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? )

Service or BroadcastReceiver?

First time I have tried to implement either of these and I am unsure which to use.
I want my application to create a time frame, e.g. 2/3/12 to 7/3/12. Multiple time frames such as this can be created. A different intervals (e.g. every 4 hours) I would like my application to preform some actions for each time frame. This needs to be done in the background.
I have first tried to implement this with a Service, but am having performing all the actions for each of the time frames concurrently. After reading the android blog "Multitasking the android way" I think that perhaps BroadcastReceivers are better.
Please advise
Please see my answer about using the AlarmManager - Running task periodicaly(once a day/once a week)
If you are only running a process at a set time rather than constantly (e.g. monitoring audio levels) then you are going to ask a service to sit there 90% of the time and do nothing except waste battery power. The AlarmManager solves this problem as it notifies the broadcast receiver to execute at the given times.
Edit: Also bear in mind that after phone restart all alarms are removed so you will need to register a broadcast receiver to be notified of the device boot-up so you can re-register any Alarms that are needed.
You should probably be using the AlarmManager and a IntentService.
The AlarmManager will kick of your IntentService at specified intervals. You can kick off the IntentService for each set of actions.

Categories

Resources