Naruto
Naruto

Reputation: 9634

create background services in android

i created a simple services, its job is to handle the incoming call. I created a services like below

public class CalldetectorService extends Service {

private CallHelper callHelper;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    int res = super.onStartCommand(intent, flags, startId);
    callHelper = new CallHelper(this);
    return res;
}

@Override
public void onDestroy() {
    super.onDestroy();

    //callHelper.stop();
}

}

Inside the helper, i m handling the call. This service is not running in background continuously. after some time, its getting halted. It works pretty well when i open app & move to background for some time, later on it wont work.

How to make my service to work in background always and catch the incoming calls

Upvotes: 1

Views: 150

Answers (2)

Frederick Nyawaya
Frederick Nyawaya

Reputation: 2305

If you don't want the service to be killed by the OS: A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

In your case, 'catching' incoming calls is best achieved with a Broadcast Receiver, registered in the manifest for intent: <action android:name="android.intent.action.PHONE_STATE" />

Upvotes: 1

tyczj
tyczj

Reputation: 73753

the answer is you dont, The OS can kill a service at anytime it needs to. you can return START_STICKY which will flag you service to be restarted when it can but there is no way to make a service run forever

Upvotes: 0

Related Questions