Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Service restarting when application is killed [duplicate]

I am new to Android. I am developing an application which logs sensor data. I have an activity with two buttons. When the user presses the start button, I want a service to be started and log sensor data until the user presses the stop button. I am not sure which type of service I want to use. I read about local vs remote services but I didn't quite actually understand the difference.

What I tried till now:

I created an activity with two buttons which start and stop a local service:

//Start Service
startService(new Intent(this, MyService.class));

//Stop Service
stopService(new Intent(this, MyService.class));

I also created a sticky service which onStartCommand() begins to log sensor data:

public int onStartCommand(Intent intent, int flags, int startId) {
    mSensorManager.registerListener(this, accelerometer,
            SensorManager.SENSOR_DELAY_FASTEST);
    mSensorManager.registerListener(this, magnetometer,
            SensorManager.SENSOR_DELAY_UI);
    mSensorManager.registerListener(this, lightSensor,
            SensorManager.SENSOR_DELAY_UI);
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return Service.START_STICKY;
}

AndroidManifest.xml:

<service android:name=".MyService" android:enabled="true" android:process=":HelloSensors_background"/>

It works well, however the problem is that when I kill the application which started the service, this service restarts and loses all the previously logged data. What shall I use in order to have a service which runs smoothly and continue to run even when the application is killed so as not to lose any logged data please?

like image 210
duncanportelli Avatar asked Mar 18 '13 09:03

duncanportelli


People also ask

What happens to service when app is killed android?

If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped.

What is startForeground in Android?

A started service can use the startForeground(int, android. app. 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.

Is onDestroy called when app is killed?

Android app doesn't call "onDestroy()" when killed (ICS)


1 Answers

After your clarification regarding the killing:

You probably want to run your Service in a different process than your Application, which you can achieve by such a declaration in your manifest:

<service
    android:name=".ServiceClassName"
    android:process=":yourappname_background" >
    ...

Use the same android:process attribute for any receiver declarations in your manifest.

If you only want to receive events which can be declared in the manifest, you can consider using an IntentService, which will almost never be visible to the user due to its short activity timespan. However, if you need to listen to events which can only be received when you register receivers programmatically (in which case, obviously, the receiver clause in the manifest makes no sense) then you cannot do anything against a user (or one of the "smart app killers") killing your service. The advantage, still, would be that users hopefully understand that your app can be killed, while your Service can't (if they want it to do something).

Additionally, you can bring your Service to the foreground.

like image 123
class stacker Avatar answered Sep 28 '22 01:09

class stacker