tags:

views:

97

answers:

2

Hi,

I would like to know if it is possible to launch my application whenever a new entry is written to phone call log (i.e. out-going, in-coming or missed call).

I wrote an application to manage the call log entries as per user preferences by listening to the publication of android.intent.action.PHONE_STATE events. My application is working fine as long as its process is running. However if the user has stopped my application with a task-killer application, then the log entries can not be managed as my app itself was not running. I need a way to make sure that my application is launched in the background if it is not already running when the android.intent.action.PHONE_STATE occurs.

Or is it possible to launch my application as a service that always runs?

Thanks in advance. Bala

+2  A: 

You can launch your application as Service:

http://developer.android.com/reference/android/app/Service.html

here is a basic service implementation:

public class MyService extends Service
{
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.i("Service", "Service is starting");
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.i("Service", "Service stopped");
    }
}

AndroidManifest.xml

<application ....>
    <service android:name=".App.MySyncService" />
</application>
Arthur
A: 

However if the user has stopped my application with a task-killer application, then the log entries can not be managed as my app itself was not running.

There is nothing that can be done about this. The same thing will occur if the user terminates your service through the Settings > Applications page.

I need a way to make sure that my application is launched in the background if it is not already running when the android.intent.action.PHONE_STATE occurs.

You can try setting up a BroadcastReceiver for this Intent in your manifest, and have it route control over to some IntentService for processing. It's the same pattern I use for AlarmManager (sample code here), just with a different trigger. I have not tried this specific Intent, though, so I cannot be certain the technique works. If it does, though, it has a huge benefit of keeping your service out of memory most of the time (IntentService shuts down when its work is complete), which will make your task-killing users happy.

Or is it possible to launch my application as a service that always runs?

No, because users are in control over their devices, not you or I.

CommonsWare