如何处理通知中的onStop回调?

时间:2019-06-19 00:30:42

标签: java android service android-notifications android-mediasession

我的代码:

mMediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public void onStop() {
            super.onStop();
            Main.unbindService(getApplicationContext());
            stopSelf();
            Log.d(TAG,"stop!");
}

我的问题是,当我的应用程序仍在运行并且我从通知中单击关闭按钮(调用onStop()媒体会话回调)时,它使我的应用程序崩溃,因为它破坏了我的 Service

我只想销毁我的服务,因为我的应用程序不再对用户可见并且在后台关闭(已从最近的应用程序中删除)。

当我单击通知中的关闭按钮时,如何防止服务被破坏?这样,当我的应用程序不再在前景 / 后台中运行并且仅可见我的通知时,仅调用Main.unbindService(getApplicationContext());stopSelf();? / p>

0 个答案:

没有答案