【发布时间】:2021-05-31 18:47:00
【问题描述】:
我只在运行 android 11 的三星设备上遇到此崩溃。显然该应用程序正在调用 startForegroundService(intent) 并要求我发布通知让用户知道我正在运行前台服务,但此调用是对 @987654322 @从未在应用程序源代码中制作,三星是否有可能制作了android 11的自定义实现并在我调用startService(intent)时自动调用startForegroundService(intent)?
堆栈跟踪
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{4ab4323 u0 {app package name}/{library package name}.player.PlayerService}
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2240)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:246)
android.app.ActivityThread.main (ActivityThread.java:8506)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
我使用context.startService(intent)启动服务,服务在应用的OnResume中启动,上下文就是应用上下文。
这也是服务在清单中的声明方式
<service
android:name=".player.PlayerService"
android:exported="false"
android:foregroundServiceType="mediaPlayback"
android:stopWithTask="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</service>
更新:我找到了调用 startForegroundService(Intent) 的原因,我正在使用来自 android 的以下接收器来帮助处理诸如耳机按钮之类的操作控制设备,所以自从我将应用程序转换为 androidx 后,它开始使用新的MediaButtonReceiver
<receiver android:name="androidx.media.session.MediaButtonReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
这是Receiver收到事件时执行的代码
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null
|| !Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())
|| !intent.hasExtra(Intent.EXTRA_KEY_EVENT)) {
Log.d(TAG, "Ignore unsupported intent: " + intent);
return;
}
ComponentName mediaButtonServiceComponentName =
getServiceComponentByAction(context, Intent.ACTION_MEDIA_BUTTON);
if (mediaButtonServiceComponentName != null) {
intent.setComponent(mediaButtonServiceComponentName);
startForegroundService(context, intent);
return;
}
ComponentName mediaBrowserServiceComponentName = getServiceComponentByAction(context,
MediaBrowserServiceCompat.SERVICE_INTERFACE);
if (mediaBrowserServiceComponentName != null) {
PendingResult pendingResult = goAsync();
Context applicationContext = context.getApplicationContext();
MediaButtonConnectionCallback connectionCallback =
new MediaButtonConnectionCallback(applicationContext, intent, pendingResult);
MediaBrowserCompat mediaBrowser = new MediaBrowserCompat(applicationContext,
mediaBrowserServiceComponentName, connectionCallback, null);
connectionCallback.setMediaBrowser(mediaBrowser);
mediaBrowser.connect();
return;
}
throw new IllegalStateException("Could not find any Service that handles "
+ Intent.ACTION_MEDIA_BUTTON + " or implements a media browser service.");
}
你可以看到它实际上启动了一个前台服务,我还在调查崩溃但至少我知道应用程序中启动了一个前台服务。
此外,这次崩溃不仅像我想的那样发生在三星,crashlytics 报告分组三星一起崩溃是因为它是来自 android 平台的崩溃,同一崩溃的其他实例发生的频率较低,因此它们在崩溃列表中的排名靠后在火力基地上。
【问题讨论】:
-
嘿,我也面临同样的问题。你有解决办法吗?
-
@vishusrivastava 您正在构建媒体播放器应用程序吗?
-
是的,应用中有播客功能。
-
您只需要确保您的应用程序在前台服务启动时发布通知。就像大多数答案所说的那样。
-
我的情况有点不同,因为我没有在应用程序中启动前台服务,但是当单击耳机的播放按钮并且我的应用程序是 androidx.media.session.MediaButtonReceiver 调用前台时最后一个播放音频的应用。
标签: android android-service samsung-mobile android-11