- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的 Android 服务中的 Context.startForegroundService() 没有调用 Service.startForeground()
,但我不明白为什么会这样。
我的应用是做流媒体的,只有当你从前台通知暂停(变成普通通知),然后你把通知滑开,这是为了停止我的服务时,才会出现这个错误。
这是调用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();
}
}
这里是设置通知删除 Intent 的地方:
.setDeleteIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this, PlaybackStateCompat.ACTION_STOP));
此 configureServiceState(long action)
方法仅从我的 MediaSession
回调调用:onPlay
、onPause
和onStop
...显然,操作是要执行的预期操作。
从我的 UI 执行 onStop
时,或者从 UI 调用 onPause
后跟 onStop
时(镜像清除通知所需的操作),仅来自通知。
关于此错误,我能找到的所有信息是,当您调用 startForegroundService
但未在 5 秒内调用 startForeground
时,它可能会发生...但是 startForeground
在唯一一次 startForegroundService
被调用后立即被调用。
此外,onPlaybackStateChange
将转到 onStop
方法中的“正在播放” Activity ,这会触发该 Activity 运行 finish()
这样服务就不会以这种方式重新启动。
我在这里错过了什么?
其他详细信息:
我的“正在播放” Activity 并未部分重启该服务,因为代码执行从未到达其任何方法。
代码执行似乎也从未在错误产生之前重新进入configureServiceState
如果我在最后可能的点添加断点(MediaButtonReceiver.handleIntent(mMediaSession, intent);
在我的服务的 onStartCommand
中),在这里暂停执行并尝试调试会导致调试器在暂停后不久断开连接
为前台尝试不同的通知 channel 与常规通知也没有区别
将暂停的通知从锁定屏幕上滑开不会导致错误,只有在手机解锁时才会发生;无论我的应用程序是否真正打开
完全异常:
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)
我的测试设备运行的是 Android 8.0,项目最小 API 为 21,目标 API 为 27。设备 API 为 26。
最佳答案
您需要为 Android O API 26 及更高版本使用 NotificationChannel,否则您将遇到您遇到的错误。请参阅 Android 文档中的以下声明 - Create and Manage Notification Channels :
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel.
这是我用于构建媒体通知的方法的摘录(从中获取您需要的内容)。您可以看到有一个针对 Android O 设备的检查,使用特定的方法来处理这种情况:
private fun compileNotification(context: Context, action: NotificationCompat.Action, mediaSession: MediaSessionCompat, controller: MediaControllerCompat, mMetadata: MediaMetadataCompat, art: Bitmap?, mPlaybackState: PlaybackStateCompat) {
val description = mMetadata.description
// https://stackoverflow.com/questions/45395669/notifications-fail-to-display-in-android-oreo-api-26
@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())
}
在您的 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)
}
我没有包含上面某些方法的详细信息,但方法名称应该是 self 注释 - 如果我可以包含更多详细信息,请告诉我。其中一些可能有些矫枉过正,但在您的情况下可能会解决问题。
我很确定这会解决您的问题。如果没有,我还有一些想法。
关于java - startForegroundService() 没有调用 startForeground(),但它调用了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50347101/
正如问题标题所问,我想知道它们之间的区别是什么,因为文档不是很清楚它们是否真的存在差异。 提前致谢。 最佳答案 ContextCompat 是用于兼容性目的的实用程序类。 context.startF
将我的应用程序更新为目标 API 27(之前为 25)后,我遇到了许多来自用户的 ANR,但我无法重现。它们似乎与 Oreo 后台执行限制有关,并带有 ANR 消息 Context.startFore
我的 Android 服务中的 Context.startForegroundService() 没有调用 Service.startForeground(),但我不明白为什么会这样。 我的应用是做流
如果我多次调用 context.startForegroundService(startServiceIntent),我会多次调用带有新服务 Intent 的 onCreate() 还是我会第一次调用
我对三星设备上的服务后台工作有疑问。 Fatal Exception: android.app.RemoteServiceException: Context.startForegroundServi
如果设备操作系统是 Android 9(Pie),我已调用 Context.startForgroundService() 但有时它会抛出错误,例如“Context.startForgroundSer
我的应用程序将在 MainActivity 的 onCreate 中调用 startForegroundService(intent)。我将 startForeground(ON_SERVICE_CO
我从 Device Monitor 得到以下异常输出: //FATAL EXCEPTION: main //Process: com.xxx.yyy, PID: 11584 //android.app
这是我的 BroadcastReciever 类。处理启动手机状态的类(class)。 代码; public class BroadCastRecieverBoot extends Broadcast
我在 Android O 操作系统上使用 Service 类。 我打算在后台使用Service。 Android documentation声明 If your app targets API lev
在检查了 google 问题和 SO 上的许多其他问题后,我找到了在底部添加的解决方案。 我负责的事情如下: 在 onCreate 和 onStartCommand 中,我确保将服务移至前台(如果它尚
我不断在标题中收到错误,我不知道该怎么办..(这是一个水提醒应用程序。)论坛中有很多荒谬的主题。没有人正确回答。我用它在设备重新启动时运行。我研究了很多问题,但没有人正确回答。 请帮助我。提前致谢!
所以我的应用程序有一些触发服务和通知的远程操作。在调用 startForegroundService 和服务尝试启动通知之间,事情可能会发生变化,因此服务会再次检查事情的状态,然后决定要做什么。 因此
我在 Android 8.0 和 Android 7.x 中都通过 Fabric.io 收到了这个错误报告。 由于不仅仅是特定类失败,我不知道如何处理它们。 如果你有任何想法,请帮助我。 最佳答案 在
注意:这个问题假设您知道将服务绑定(bind)到上下文。 制作中的每个人都知道这个问题,甚至一些拥有 Android Oreo 或 Pie 设备的用户都知道。异常如下所示: Fatal Excepti
我目前正在研究如何创建一个 float 的前台泡泡聊天头服务。 但是,我注意到我尝试使用的所有库都不适用于 API-28。 我相信这是由于提到的新限制 here in the Android docs
我正在使用的应用程序(在 Android O 中)将在设备重启后启动服务。设备重启后,在广播接收器的 onReceive() 方法中,它会调用服务作为 Android OS 8 及更高版本的 star
我注意到 Pixel 5 和 Pixel 4a(都在 Android 12 上)的一个异常(exception)(Firebase Crashlytics),没有其他设备,只发生了两次,每个设备一次。
我有点困惑,因为我读了一些帖子,如果 API >= 26,我也应该使用 ContextCompat.StartForegroundService();。 现在我仍然只使用 StartService 并
首先,我看了这些; Context.startForegroundService() did not then callService.startForeground() Context.startF
我是一名优秀的程序员,十分优秀!