服务会被破坏掉了 [英] Service keeps getting destroyed

查看:152
本文介绍了服务会被破坏掉了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用媒体播放器播放一些音乐的服务。该服务由一个活动开始,如果事情正在播放用户从服务应保持在后台播放活动移开。
运行在前台服务似乎工作(我可以看到该通知),但在接近服务立即销毁所有情况下(通过的OnDestroy对服务系统调用)。
我知道使用startForeground并不意味着该服务不会导致死亡,但它一直被破坏掉了马上,所以我猜太少ressources强制系统杀死它,是不是其中的原因。

I have a service that uses media player to play some music. The service is started by an activity and if something is playing and the user moves away from the activity the service should keep playing in the background. Running the service in the foreground seems to work (I can see the notification), but in close to all cases the service is destroyed immediately (OnDestroy called by the system on the service). I know using startForeground does not mean that the service is never killed, but it keeps getting destroyed right away, so I guess too few ressources forcing the system to kill it, is not the reason for that.

这是我如何实现它:
在活动的OnCreate中,我开始(在后台)和绑定服务。在我的onPause使服务的前景不被摧毁,以及:

This is how I implemented it: In OnCreate of the activity, I start (in the background) and bind the service. In OnPause I bring the service to the foreground to not get destroyed as well:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);

    // start service
    startService(new Intent(this, MyService.class));
    // connect to service
    bindToService();
    ...
}

@Override
protected void onDestroy() {
    unbindFromService();
    super.onDestroy();
};

@Override
protected void onStart() {
    super.onStart();
}

@Override
protected void onStop() {
    super.onStop();
}

@Override
protected void onPause() {
    super.onPause();

    if (MediaPlayerService.getInstance().getStatus() == MEDIA_PLAYER_STATUS.Started) {
        // current playing something => keep service running
        mService.startForeground();
    } else {
        // stop service
        stopService(new Intent(MainActivity.this, MyService.class));
    }
}

@Override
protected void onResume() {
    super.onResume();

    // remove service from foreground
    if (mService != null) {
        mService.stopForeground();
    }
}

void bindToService() {
    // Establish a connection with the service. We use an explicit
    // class name because we want a specific service implementation that
    // we know will be running in our own process (and thus won't be
    // supporting component replacement by other applications).
    bindService(new Intent(MainActivity.this, MyService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

void unbindFromService() {
    if (mIsBound) {
        // Detach our existing connection.
        unbindService(mConnection);
        mIsBound = false;
    }
}

private final ServiceConnection mConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the service object we can use to
        // interact with the service. Because we have bound to a explicit
        // service that we know is running in our own process, we can
        // cast its IBinder to a concrete class and directly access it.
        mService = ((MyService.LocalBinder) service).getService();
        mService.registerClient(MainActivity.this);
    }

    @Override
    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        // Because it is running in our same process, we should never
        // see this happen.
        mService = null;
        mService.unRegisterClient(MainActivity.this);
    }
};

在我服务的启动/ stopForeground功能是这样的:

And the start/stopForeground functions in my Service look like this:

public void startForeground() {
    String songName = "blabla";
    // assign the song name to songName
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), MainActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification();
    notification.tickerText = songName;
    notification.icon = R.drawable.ic_launcher;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(getApplicationContext(), "MusicPlayerSample",
            "Playing: " + songName, pi);
    startForeground(NOTIFICATION_ID, notification);
}

public void stopForeground() {
    stopForeground(true);
}

为什么,如果我从活动搬走服务不断得到破坏任何想法?

Any ideas why the service keeps getting destroyed if I move away from the activity?

推荐答案

问题是绑定/解除绑定中的onCreate /的onDestroy。
我真的不能解释它,但在生命周期状态结合导致越来越即使您使用活动的startService()中的onCreate()服务销毁。

The problem is the bind/unbind in onCreate/onDestroy. I can't really explain it, but binding in that lifecycle states leads to getting the service destroyed even though you use startService() in onCreate() of the activity.

此安装程序现在工作得很好:

This setup now works nicely:


  • 在活动的onCreate()使用startService()来获得该服务启动。

  • 服务的onStartCommand()必须返回Service.START_STICKY

  • 在活动的onResume()绑定到该服务

  • 在活动的的onPause()如果事情是玩,调用该服务的startForeground()和它解除绑定。

  • 在活动的的onDestroy()调用stopService(),如果没有当前正在播放。

这篇关于服务会被破坏掉了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆