Fcoder
Fcoder

Reputation: 9216

starting an activity stops android service

i have an android service that plays music.i start in my main activity with this code:

Intent service = new Intent(MainMenu.this, musicservice.class);
MainMenu.this.startService(service); 

and this is my service:

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    MediaPlayer mp;
    mp = MediaPlayer.create(musicservice.this, R.raw.music);
    mp.setOnCompletionListener(new OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });   
    mp.start();

    return Service.START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

}

it stsrts work fine, but when i start another activity, my music goes off and seems my service destroys!! but dont want this, i want my service only stops when my application ends. music plays only when user works with app. even when app in in the background i want my music dont play! how i can implement this?

Upvotes: 0

Views: 727

Answers (2)

Ajay S
Ajay S

Reputation: 48592

Service destroy when your activity destroy when you start service from activity.

Use AlarmManager to schedule the service repeatedly.

Upvotes: 0

tralfamadore
tralfamadore

Reputation: 56

If you want your application to play music while your activity is on, try bind it.

it should look something like this:

Service:

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;

import com.example.playmusic.R;

public class PlayMusicService extends Service {

    private final IBinder binder = new LocalBinder();

    private MediaPlayer player;

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public class LocalBinder extends Binder {

        public PlayMusicService getService() {
            return PlayMusicService.this;
        }
    }

    public void play() {
        player = MediaPlayer.create(this, R.raw.music);
        player.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
        player.start();

    }

    public void pause() {
        player.pause();
    }
}

Activity:

package com.example.playmusic;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.example.service.PlayMusicService;
import com.example.service.PlayMusicService.LocalBinder;

public class MainActivity extends Activity {

private PlayMusicService service;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
protected void onPause() {
    super.onPause();
    service.pause();
}

@Override
protected void onStart() {
    super.onStart();
    if (service != null) {
        service.play();
    } else {
        bindService();
    }
}

    private void bindService() {
        bindService(new Intent(this, PlayMusicService.class), new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
                service = null;

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                LocalBinder localBinder = (LocalBinder) binder;
                service = localBinder.getService();
                service.play();

            }
        }, Context.BIND_AUTO_CREATE);
    }

}

Upvotes: 1

Related Questions