How to continue playing music when I killed App
<p>In Android,I use Service and BroadcastReceiver to play music.</p>

<p>The MediaPlayer can continue playing music when I press Home Button or Back Button.<strong>But it can not works when I removed it from Overview Screen.</strong> Even If I set the Service as Foreground Service.</p>

<p><strong>@0xDEADC0DE points out that the id shouldn't be 0.</strong> I modify it and now it can work well.But it must have a notification.Some Apps such like NetEase Cloud Music,which hasn't any notification and can play even if it be killed,how it works?</p>

<p><strong>update:</strong>
I found that I have closed that app's notification,so it seems still playing music without notification when I kill it....So the problem resolved,thanks </p>

<p>Here is my code:</p>

<p><strong>MainActivity.java</strong> ,</p>

<pre><code>public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ImageButton mPlayButton;
private ImageButton mPreviousButton;
private ImageButton mNextButton;
private TextView mMusicName;
private boolean mState = false;
private Intent intent;

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

    mMusicName = (TextView) findViewById(R.id.music_name);
    mPlayButton = (ImageButton) findViewById(R.id.play);
    mNextButton = (ImageButton) findViewById(R.id.next);
    mPreviousButton = (ImageButton) findViewById(R.id.previous);
    mPlayButton.setOnClickListener(this);
    mNextButton.setOnClickListener(this);
    mPreviousButton.setOnClickListener(this);


    intent = new Intent(this, MusicService.class);

    startService(intent);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.play:
            if (!mState) {
                //start
                mState = true;
                mPlayButton.setImageResource(R.mipmap.ic_pause_circle_outline_white_48dp);
                Intent intent = new Intent();
                intent.setAction("com.geekband.mo.musicplayer");
                intent.putExtra("action", 0);
                sendBroadcast(intent);

            } else {
                //pause
                mPlayButton.setImageResource(R.mipmap.ic_play_circle_outline_white_48dp);
                mState = false;
                Intent intent = new Intent();
                intent.setAction("com.geekband.mo.musicplayer");
                intent.putExtra("action", 1);
                sendBroadcast(intent);
            }
            break;

        case R.id.next:
            mState = true;
            mPlayButton.setImageResource(R.mipmap.ic_pause_circle_outline_white_48dp);
            Intent intent = new Intent();
            intent.setAction("com.geekband.mo.musicplayer");
            intent.putExtra("action", 2);
            sendBroadcast(intent);
            break;

        case R.id.previous:
            mState = true;
            mPlayButton.setImageResource(R.mipmap.ic_pause_circle_outline_white_48dp);
            Intent intent2 = new Intent();
            intent2.setAction("com.geekband.mo.musicplayer");
            intent2.putExtra("action", 3);
            sendBroadcast(intent2);
            break;

    }
    }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_exit) {
        stopService(intent);
        finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
</code></pre>

<p><strong>Service.java</strong> ,</p>

<pre><code>public class MusicService extends Service {


private MediaPlayer mPlayer;
private MusicBroadcastReceiver mBroadcastReceiver;

@Override
public void onTaskRemoved(Intent rootIntent) {
    Log.d("MusicService", "onTaskRemoved");
}

private void pause() {
    if (mPlayer != null) {
        mPlayer.pause();
    }
}

private void previous() {
    if (mPlayer != null) {
        mPlayer.release();
    }
    mPlayer = MediaPlayer.create(MusicService.this, R.raw.loststars);
    mPlayer.start();
}

private void next() {
    if (mPlayer != null) {
        mPlayer.release();
    }
    mPlayer = MediaPlayer.create(MusicService.this, R.raw.nanshannan);
    mPlayer.start();
}

private void start() {
    if (mPlayer != null) {
        mPlayer.start();
    }
}

@Override
public void onCreate() {
    super.onCreate();

    mBroadcastReceiver = new MusicBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("com.geekband.mo.musicplayer");
    registerReceiver(mBroadcastReceiver, intentFilter);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mPlayer = MediaPlayer.create(MusicService.this, R.raw.loststars);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification noti = new Notification.Builder(MusicService.this)
            .setContentTitle("play")
            .setContentText("playing")
            .setSmallIcon(R.mipmap.ic_launcher)
            .build();
    // manager.notify(0, noti);
    // startForeground(0, noti);
    startForeground(1, noti);
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    if (mPlayer != null) {
        mPlayer.release();
    }
    super.onDestroy();
}

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

public class MusicBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int action = intent.getIntExtra("action",0);
        switch (action) {
            case 0:
                start();
                break;

            case 1:
                pause();
                break;

            case 2:
                next();
                break;

            case 3:
                previous();
                break;

        }
    }
}
}
</code></pre>