0

I created a service class that should run in background.
And it is working well.
The problem is when i remove task from recent panel it restarts and i loose all data stored in my service class.

I tried almost every way available on internet.
Search for a music player named Phonograph
It does just what i want, song keeps playing without any pause even after removing it from overview screen.

Service Class

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

Manifest

<service
    android:name=".service.MS"
    android:enabled="true"
    android:process="com.architjn.ms"></service>

-> Don't mark it as duplicate as i already tried previously asked similar questions, non of them worked for me.

architjn
  • 1,397
  • 2
  • 13
  • 30

1 Answers1

1

You should use startForeground in your service http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)

Here is an example that maybe useful

https://github.com/imjarp/101AndroidExamples/blob/cbc64af4748f1f8876138ac077216fd8ab19840d/15-ParallelExecution/app/src/main/java/com/example/jarp/parallelexecution/MediaTranscoder.java

JARP
  • 1,239
  • 12
  • 12
  • Thank you that worked as i want, one more question, how do i get to know that service is already running. `if(startService(i)==null)` starts the service. – architjn Jul 01 '15 at 04:27
  • Great ! you could use bindService() see this example [bindService](http://stackoverflow.com/questions/8341667/bind-unbind-service-example-android) – JARP Jul 01 '15 at 04:29