PaulG
PaulG

Reputation: 7102

Android service won't run in background

I tried creating an Android Service yesterday for the first time. We basically need a mechanism that checks the state of our servers every 30 minutes. I've looked at 4 or 5 tutorials with a lot of good comments, but I can't seem to get my Service to work at all.

Following one tutorial, I managed to get the service running and print to the log once a minute, which worked great. The thing is when I exit the app (I mean by hitting the BACK button until the app closes, or even by hitting the home key) the service seems to stop.

If my code wasn't such a mess I would post some. I basically want the service to start when the user first launches the app (or hits a specific button, it doesn't really matter), and run continuously no matter if the user exits out of the app or not, and perform it's server checks every half hour, and possibly play a sound in some cases (again, whether the app is visually running or not) .Can anyone provide any pseudo code for what I'm trying to achieve? And perhaps some more details about the specific methods I should implement and when to call them?

Thanks a lot. I would like to get this done before the long weekend if possible.

Upvotes: 1

Views: 3225

Answers (2)

Ben Joseph
Ben Joseph

Reputation: 1

**

 - package com.Touch; import android.app.Service; import
   android.content.Intent; import android.media.MediaPlayer; import
   android.os.IBinder; public class BackServices extends Service {
       @Override
       public IBinder onBind(Intent arg0) {
           return null;
       }
       @Override
       public void onCreate() {
       }
       @Override
       public void onDestroy() {
       }
       @Override
       public void onStart(Intent intent, int startid) {
       } }

**

Upvotes: 0

coder_For_Life22
coder_For_Life22

Reputation: 26971

Check out this tutorial Running a Service in the Background.

It seems to be exactly what you need.

Also you will need to have a broadcastreceiever so that when the device reboots your service will start.(if you want this)

Check out the answer on this thread.

Android app with Service only

Upvotes: 1

Related Questions