11

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?

duncanportelli
  • 3,161
  • 8
  • 38
  • 59
  • 1
    What do you mean by kill the application? If the user goes to the settings page for the apps and chooses 'Force stop' the service will be stopped, there is nothing that can be done about it. – Premsuraj Mar 18 '13 at 10:03
  • [This](http://www.youtube.com/watch?v=XVyXCrM-3zw) is what I mean by killing the application – duncanportelli Mar 18 '13 at 10:11
  • I second the question _what do you mean by "kill the application_"? However, I'd like to point out that the service _can_ be separated from the app in such a way that if the user stops the _app_, the _service_ can continue to run. – class stacker Mar 18 '13 at 10:11
  • @ClassStacker I pointed out how "I kill the application" by putting a link. Yes, what I need is a service which continues to run even though the app is stopped. What I have now is a service which although it continues to run, it is restarted when he app is stopped. – duncanportelli Mar 18 '13 at 10:14
  • I have provided an answer. I'm not sure if I understand "restarted when the app is stopped" correctly. Can you describe that in more detail and provide the code with which you start and stop the service _in its context_, i.e. the lifecycle methods of your Activity. – class stacker Mar 18 '13 at 10:23
  • @ClassStacker I check this by going to Settings --> Apps --> Running Services on my phone. When I kill the application as shown in the video, the service in the settings is shown as restarting, even after I addeed the `android:process` attribute – duncanportelli Mar 18 '13 at 10:27

2 Answers2

11

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.

class stacker
  • 5,357
  • 2
  • 32
  • 65
-3

I think you have to declare the service in the AndroidManifest.xml.

  • 1
    True; and how does that relate to the question? – class stacker Mar 18 '13 at 10:11
  • Well, it was the same as what you provided with the exception of no code from me (I don't have my own code at hand to access). I have to do something similar in one of my apps, which is why I didn't require the clarification; I recognised what the user was after. In saying that, I couldn't have provided as detailed an answer as you as I am still fairly new to Android. I could have explained the reasons for the requirement though. Sorry about that. – confused_at_times Mar 18 '13 at 11:45
  • this answer is misleading and did not relate to the question. – ralphgabb Aug 28 '19 at 07:26