android service设置persistent,Persistent Service on Android

I am trying to write a service that comes with a MediaPlayer. I have different Activities accessing it, so I thought it would be best to peruse a Service.

It works fine so far, I have also added a call to startForeground, as described here. The notification shows up.

But when I now press the home or back button on the device, the service is stopped and onDestroy is called, and the notification icon disappears. When I return, the service seems to reBind just fine.

I stop the music playback on onDestroy, so of course it stops. But I would like to keep the notification and service alive even when the user is on another app.

EDIT: I hope this is the relevant part:

public class MediaPlayerService extends Service {

private static class PlayerMessageHandler extends Handler {

private final MediaPlayerService owner;

public PlayerMessageHandler(MediaPlayerService owner) {

this.owner = owner;

}

@Override

public void handleMessage(Message msg) {

// Handle

}

}

private static final int NOTIFICATION_ID = 13138;

private final Messenger messenger = new Messenger(new PlayerMessageHandler(

this));

private MediaPlayer player;

private Notification notification;

@Override

public IBinder onBind(Intent intent) {

startNotification();

return messenger.getBinder();

}

@Override

public void onCreate() {

super.onCreate();

Log.v(TAG, "Media player service created.");

player = new AudiobookPlayer(this);

new Thread(seekerUpdate).start();

isRunning = true;

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.v(TAG, "Received start id " + startId + ": " + intent);

return START_STICKY;

}

@Override

public void onDestroy() {

super.onDestroy();

Log.v(TAG, "Media player service destroyed.");

if (player.isPlaying()) {

player.pause();

}

sendMessageToUI(MSG_PLAYER_HAS_PAUSED);

isRunning = false;

}

private void sendMessageToUI(int msg) {

Log.v(TAG, "Sending " + msg);

sendMessage(Message.obtain(null, msg));

}

private void sendMessage(final Message message) {

// Send

}

private void startNotification() {

NotificationCompat.Builder builder = new NotificationCompat.Builder(

this);

builder.setSmallIcon(R.drawable.notification);

builder.setContentTitle(getString(R.string.app_name));

notification = builder.build();

startForeground(NOTIFICATION_ID, notification);

}

}

EDIT2: Methods from the activity, taken from here

@Override

protected void onStart() {

super.onStart();

// Bind to the service

bindService(new Intent(this, MediaPlayerService.class),

playerServiceConnection, Context.BIND_AUTO_CREATE);

}

@Override

protected void onStop() {

super.onStop();

// Unbind from the service

if (bound) {

unbindService(playerServiceConnection);

bound = false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值