2

I build an application that collects data about the battery. In order to collect this data I need my application to run on the background in order to be able to collect this data. How can I do it?

Alex K
  • 5,092
  • 15
  • 50
  • 77

3 Answers3

4

You need to change your activities into Service

Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • Can you be more specific please? I want to create an app that collects info about the battery (all the time). How can I do? when I tried to read the link you gave me I got lost in all the information their... – Alex K Jun 21 '11 at 17:33
  • A context that has no window, is called Service. You need to read more about Service. – Pentium10 Jun 21 '11 at 18:49
  • Maybe I didn't explained my self enough. I need to collect the info and when I open the app it displayes the info it collected. – Alex K Jun 21 '11 at 19:06
  • You collect the info in background by using a Service, and you save to database. When the user opens your app, it will get a screen with the data retrieved from the database. – Pentium10 Jun 22 '11 at 08:03
  • @Pentium10 Hey, how do I start up my background service on boot? – Ruchir Baronia Jan 10 '16 at 16:57
  • http://stackoverflow.com/questions/11168869/starting-background-service-when-android-turns-on – Pentium10 Jan 11 '16 at 08:46
0
 - @Override public int onStartCommand(Intent intent, int flags, int
   startId) {
       handleCommand(intent);
       // We want this service to continue running until it is explicitly
       // stopped, so return sticky.
       return START_STICKY; }
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
0

if you're against using a service for whatever reason you can have it thread off, then get the data when the user calls the application to the front.

you can use the onStart, onPause, onResume functions as well as making the application single instance so when you run it again, it mearly pulls it up from memory (assuming Android doesn't kill it for some reason).

You can use ongoing notification to prevent it from being killed in the background and moveTaskToBack.

But as pentium10 says, the intended way to handle background processes is through a service which gathers the data you are looking for, then when the activity comes back to the front, it gets the data from the service and displays it.

EboMike
  • 76,846
  • 14
  • 164
  • 167
Dakun Skye
  • 580
  • 1
  • 4
  • 14