1

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

Naruto
  • 9,476
  • 37
  • 118
  • 201

2 Answers2

1

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" />

Frederick Nyawaya
  • 2,285
  • 1
  • 15
  • 19
  • Hi, thank you. What about for outgoing calls?, i need to record the outgoing call number also when user dials. For this broadcastreceiver wont work i guess – Naruto Oct 16 '14 at 16:53
  • Use a Broadcast Receiver, with this as the intent-filter: `` – Frederick Nyawaya Oct 16 '14 at 16:54
  • Oh, in this case both of my incoming and outgoing calls number can be recorded, irrespective of my app runs in background or not right. i'll try your trick. thanks.. ill get back to you shortly – Naruto Oct 16 '14 at 16:56
  • Don't forget the permissions, `` is one of them – Frederick Nyawaya Oct 16 '14 at 16:58
  • Hi, in receiver how to check its incoming/outgoing call. i.e how will i get to know from intent, any idea? – Naruto Oct 17 '14 at 18:04
0

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

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Hi, Thank you for your answer, can you please tell me in my case how to handle the incoming call event from my app. – Naruto Oct 16 '14 at 16:43
  • look here http://stackoverflow.com/questions/1853220/retrieve-incoming-calls-phone-number-in-android – tyczj Oct 16 '14 at 16:44
  • Thank you, Now you are telling me to use broadcastreceivers instead of services right?, i have to code to handle calls i.e both incoming and outgoing. But if i kill activity or if i put my activity in background for longer period, this wont work. I'm seeking your help on this – Naruto Oct 16 '14 at 16:47