我Service
在Android O OS上使用Class.
我计划Service
在后台使用.
Android建议指出startService()
应该使用startForegroundService()
.
如果您使用startForegroundService()
,则Service
抛出a Service
然后调用Service
错误.
这有什么问题?
service android operating-system foreground android-8.0-oreo
首先,我看了看这些;
我有将近一百万人使用的流应用程序。我正在为播放器使用前台服务。我还没有实现MediaSession
。我有99.95%的无崩溃会话。因此,该应用程序适用于所有版本,但我开始使用Android 9获取崩溃报告(ANR)。此崩溃仅发生在Samsung
手机上,尤其是s9, s9+, s10, s10+, note9
型号。
我尝试过这些
startForeground()
方法onCreate()
Service.startForeground()
之前致电Context.stopService()
我阅读了Google开发人员的一些评论,他们说这仅仅是Intended Behavior
。我想知道这是由三星的系统还是Android操作系统引起的。有人对此有意见吗?我怎样才能解决这个问题?
我的用户报告了数百次崩溃,但仍然无法找到解决方法.这些崩溃来自Android 8(三星,华为,谷歌).
我得到这两个崩溃:
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Run Code Online (Sandbox Code Playgroud)
另一个:
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2104)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7428)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Run Code Online (Sandbox Code Playgroud)
我假设这些崩溃是相同的,但正如您所看到的,堆栈跟踪显示不同的代码行.
问题是我无法重现它,我的设备和模拟器上的一切正常.但是,我(不知何故)通过创建服务而不调用类startForeground()
内来复制Service
.
我无法"捕获"异常,因为它在创建服务5秒后立即来自系统级别.
我做了什么,我创建了一个方法,它创建一个粘性通知并调用startForeground
方法(我的服务类):
private void startWithNotification() {
Resources res = getResources();
String title = res.getString(R.string.application_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}
NotificationCompat.Builder …
Run Code Online (Sandbox Code Playgroud) 我对三星设备上的服务的后台工作有问题。
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Run Code Online (Sandbox Code Playgroud)
当然,启动服务后,我会调用Service.startForeground(),而在其他设备上就没有问题,仅三星设备就没有问题。
有人知道这个问题的原因和解决方法吗?
我在Zopim SDK https://chat.zendesk.com/hc/en-us/community/posts/360004395368-Crash-on-Android-8-in-Android-SDK-1-4-2中发现了类似的问题
这是我的BroadcastReciever类。该类正在处理启动电话状态。
代码;
public class BroadCastRecieverBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent ?ntent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(?ntent.getAction()))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, MyService.class));
context.startForegroundService(new Intent(context, GPSTracker.class));
} else {
context.startService(new Intent(context, MyService.class));
context.startService(new Intent(context, GPSTracker.class));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误;
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Run Code Online (Sandbox Code Playgroud)
现在,它不适用于Android Oreo。我不知道那是什么错误。
注意:此问题假设您知道将服务绑定到上下文。
生产中的每个人,甚至一些拥有 Android Oreo 或 Pie 设备的用户都意识到了这个问题。异常看起来像这样:
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Run Code Online (Sandbox Code Playgroud)
当你打电话时,Context.startForegroundService(Intent)
你也必须Service.startForeground(int, Notification)
在 5 秒内打电话(即使你的手机可能甚至无法启动你的服务,但由于某种原因它成为你的错),否则你的应用程序将冻结或从 Android 框架抛出异常。经过进一步思考,我制定了一个解决方案。
由于Context.startForegroundService(Intent)
不保证服务将在 5 秒内创建并且是异步操作,因此我认为事情是这样进行的:
// Starts service.
public static void startService(Context context)
{
// If the context is null, bail.
if (context == null)
return;
// This is the same as checking Build.VERSION.SDK_INT >= Build.VERSION_CODES_O
if (Utilities.ATLEAST_OREO)
{ …
Run Code Online (Sandbox Code Playgroud) android android-service android-service-binding foreground-service