tra*_*ary 10 java android android-notifications foreground-service remoteserviceexception
我有Context.startForegroundService() did not then call Service.startForeground()我的Android服务,但我无法弄清楚它为什么会发生.
我的应用程序用于媒体流,只有当您从前台通知暂停(将其转换为常规通知)时才会出现此错误,然后您将通知刷掉,这是为了停止我的服务.
这里就是唯一的方法startForegroundService,startForeground而stopForeground方法被称为:
private void configureServiceState(long action) {
if (action == PlaybackStateCompat.ACTION_PLAY) {
if (!mServiceInStartedState) {
ContextCompat.startForegroundService(
StreamingService.this,
new Intent(
StreamingService.this,
StreamingService.class));
mServiceInStartedState = true;
} startForeground(NOTIFICATION_ID,
buildNotification(PlaybackStateCompat.ACTION_PAUSE));
} else if (action == PlaybackStateCompat.ACTION_PAUSE) {
stopForeground(false);
NotificationManager mNotificationManager
= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert mNotificationManager != null;
mNotificationManager
.notify(NOTIFICATION_ID,
buildNotification(PlaybackStateCompat.ACTION_PLAY));
} else if (action == PlaybackStateCompat.ACTION_STOP) {
mServiceInStartedState = false;
stopForeground(true);
stopSelf();
}
}
Run Code Online (Sandbox Code Playgroud)
这里是我的通知的删除意图设置的地方:
.setDeleteIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_STOP));
Run Code Online (Sandbox Code Playgroud)
这种configureServiceState(long action)方法只从我叫MediaSession:回调onPlay,onPause并onStop用行动是要执行的意图动作......明显.
onStop从我的UI 执行时,或者从UI(镜像清除通知所需的操作)调用onPause后,onStop仅从通知中执行时不会发生错误.
我可以找到关于这个错误的所有内容,它可能是在你打电话startForegroundService但没有startForeground在5秒内调用时发生的......但是在startForeground调用唯一的时间后立即startForegroundService调用.
此外,该onPlaybackStateChange方法将进入我的"正在播放"活动onStop,该活动触发该活动运行,finish()以便不以这种方式重新启动服务.
我在这里错过了什么?
额外细节:
由于代码执行从未到达任何方法,因此我的"正在播放"活动不会部分重新启动该服务.
configureServiceState在生成错误之前,代码执行似乎也从未重新进入
如果我在最后一个可能点(MediaButtonReceiver.handleIntent(mMediaSession, intent);在onStartCommand我的服务中)添加一个断点,暂停执行并尝试调试会导致调试器在暂停后立即断开连接
为前台和常规通知尝试不同的通知渠道也没有区别
将暂停的通知从锁定屏幕上滑动不会导致错误,只有在手机解锁时才会出现错误.无论我的应用程序是否实际打开
完全例外:
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Run Code Online (Sandbox Code Playgroud)
我的测试设备运行的是Android 8.0,项目最小API为21,目标API为27.设备API为26.
您需要为Android O API 26及更高版本使用NotificationChannel,否则将遇到错误。请参阅Android文档中的以下声明- 创建和管理通知渠道:
从Android 8.0(API级别26)开始,必须将所有通知分配给一个频道。
这是我用来构建媒体通知的方法的摘录(摘录自您所需要的)。您会看到有针对Android O设备的检查,其中包含处理该情况的特定方法:
private fun compileNotification(context: Context, action: NotificationCompat.Action, mediaSession: MediaSessionCompat, controller: MediaControllerCompat, mMetadata: MediaMetadataCompat, art: Bitmap?, mPlaybackState: PlaybackStateCompat) {
val description = mMetadata.description
// /sf/ask/3177696861/
@TargetApi(26)
if(Utils.hasO()) {
val channelA = mNotificationManager?.getNotificationChannel(NotificationChannelID.MEDIA_SERVICE.name)
if(channelA == null) {
val channelB = NotificationChannel(NotificationChannelID.MEDIA_SERVICE.name,
"MediaService",
NotificationManager.IMPORTANCE_DEFAULT)
channelB.setSound(null, null)
mNotificationManager?.createNotificationChannel(channelB)
}
}
val notificationBuilder = if(Utils.hasLollipop()) {
NotificationCompat.Builder(context, NotificationChannelID.MEDIA_SERVICE.name)
} else {
NotificationCompat.Builder(context)
}
notificationBuilder
.setStyle(android.support.v4.media.app.NotificationCompat.MediaStyle()
// Show actions 0,2,4 in compact view
.setShowActionsInCompactView(0,2,4)
.setMediaSession(mediaSession.sessionToken))
.setSmallIcon(R.drawable.logo_icon)
.setShowWhen(false)
.setContentIntent(controller.sessionActivity)
.setContentTitle(description.title)
.setContentText(description.description)
.setLargeIcon(art)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOngoing(mPlaybackState.state == PlaybackStateCompat.STATE_PLAYING)
.setOnlyAlertOnce(true)
if(!Utils.hasLollipop()) {
notificationBuilder
.setStyle(android.support.v4.media.app.NotificationCompat.MediaStyle()
// Show actions 0,2,4 in compact view
.setShowActionsInCompactView(0,2,4)
.setMediaSession(mediaSession.sessionToken)
.setShowCancelButton(true)
.setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_STOP)))
// Stop the service when the notification is swiped away
.setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_STOP))
}
notificationBuilder.addAction(NotificationCompat.Action(
R.drawable.exo_controls_previous,
"Previous",
MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)))
notificationBuilder.addAction(NotificationCompat.Action(
R.drawable.ic_replay_10_white_24dp,
"Rewind",
MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_REWIND)))
notificationBuilder.addAction(action)
notificationBuilder.addAction(NotificationCompat.Action(
R.drawable.ic_forward_10_white_24dp,
"Fast Foward",
MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_FAST_FORWARD)))
notificationBuilder.addAction(NotificationCompat.Action(
R.drawable.exo_controls_next,
"Next",
MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_SKIP_TO_NEXT)))
(context as MediaService).startForeground(NOTIFICATION_ID, notificationBuilder.build())
}
Run Code Online (Sandbox Code Playgroud)
在onStop()回调中,您需要stopSelf()在服务内调用。然后,在onDestroy()调用服务方法时,您需要进行一些清理(适用于您的情况),如下所示:
override fun onDestroy() {
super.onDestroy()
abandonAudioFocus()
unregisterReceiver(mNoisyReceiver)
//Deactivate session
mSession.isActive = false
mSession.release()
NotificationManagerCompat.from(this).cancelAll()
if(mWiFiLock?.isHeld == true) mWiFiLock?.release()
stopForeground(true)
}
Run Code Online (Sandbox Code Playgroud)
我没有包括上述某些方法的详细信息,但是方法名称应该是自注释的-让我知道是否可以包含更多详细信息。他们中的一些人可能会矫kill过正,但您的情况可能会解决问题。
我很确定这可以解决您的问题。如果没有,我还有其他想法。
| 归档时间: |
|
| 查看次数: |
5197 次 |
| 最近记录: |