vendredi 24 juin 2016

Why does service destroy when main process stops?

I am learning services these days and just experimenting around it and I don't have any intention to make Music player but just to learn about how do services work. So let me explain what I am doing.

My application is having one Activity and one Service extending Service class in which I have

@Override
public void onCreate()
{
    mediaPlayer=MediaPlayer.create(this,R.raw.serenity);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    mediaPlayer.start();
    return 0;
}

And in my MainActivity class I am starting the Service on the main thread by

startService(intent);

And also I have added the attribute

<service
        android:name=".MusicService"
        android:process=":music"/>

To run the service in another process. Now I want to say that when I am starting the service with a button on activity class the service starts playing music although when i press back button the activity destroys and also music still play yeah that what I want but when I remove this app from recents then The music stops and get start with the beginning. Please tell me why this is happening. I don't want to music stop I want it to just play independent of activity or process of the application.

Aucun commentaire:

Enregistrer un commentaire