日志如下:
Time: 2025-05-12 23:41:07
Brand: Xiaomi
Model: 2211133C
Android Version: 13 (SDK 33)
Exception:
Message:Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{5105f70 u0 com.android.app/.MQTTService}
Stack Trace:
android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{5105f70 u0 com.android.app/.MQTTService}
at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2078)
at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2052)
at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2314)
at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:210)
at android.os.Looper.loop(Looper.java:299)
at android.app.ActivityThread.main(ActivityThread.java:8240)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:559)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:954)
从日志看,大概意思是启动前台服务失败,问题时,我程序里根本没有启动前台服务,引用的第三方库,也不会启动前台服务。
这个问题,仅在这个用户出现。
而且诡异的是,正常情况下,app 连续崩溃几次,安卓就会禁止自启动,而这个用户的手机,居然可以一直在自启动,启动后后崩溃,然后继续自启动。
有没有安卓开发的大佬,这是属于什么问题?
![]() |
1
dingwen07 2 天前
让用户把你的 App 禁用后台优化试试看
|
![]() |
2
Musong 2 天前
“引用的第三方库,也不会启动前台服务。”,感觉是这的问题,可能有推送之类的。
纯猜测阿 |
![]() |
3
wuruxu 2 天前
应该先问 AI ,一般问题都能搞定的
|
![]() |
5
yafoo OP @Musong 我这个 app 本身是做推送的,引用的第三方库,只有简单的几个,一个 http 请求库,一个谷歌的 json 解析库,一个开源的 mqty 客户端,一个 markdown 解析库,所以说不会是第三方库引起的。
app 虽然没几个用户,不过已经迭代了 2 年了,目前只有这一个用户出现这情况。 |
![]() |
8
yafoo OP 忘了说了,我自身代码里是启动的后台服务
|
9
nightlight9 2 天前
@yafoo #6 日志里都写了 MQTTService 是前台服务....
|
![]() |
10
maokg 2 天前
android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{5105f70 u0 com.android.app/.MQTTServic
|
![]() |
11
rumengzhenxing 2 天前
@yafoo #8
~~~java @Override public void onCreate() { super.onCreate(); // 创建通知渠道( Android 8.0+必需) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( "MQTT_CHANNEL_ID", "MQTT Service", NotificationManager.IMPORTANCE_LOW ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } // 构建通知并启动前台服务 Notification notification = new NotificationCompat.Builder(this, "MQTT_CHANNEL_ID") .setContentTitle("MQTT 服务运行中") .setContentText("正在连接服务器...") .setSmallIcon(R.drawable.ic_notification) .build(); startForeground(1, notification); // ID 必须非 0 ,通知不可为 null } ~~~ |
![]() |
12
winterbells 2 天前
全局搜 MQTTService ,ctrl + shift + f 搜,切换到 scope all place
|
13
capric 2 天前
mqtt clientl 连接 broker 超时了?
|
![]() |
14
debuggeeker 2 天前
启动的后台服务,那就对了,启动后台服务,高版本需要 startForeground ,还需要带个通知显示,查一查官方 api 吧,需要配置权限和服务通知。
|
![]() |
15
Bichat 2 天前
以前接过 android 版本的 mqtt 库,他会创建一个前台 service 的
|
![]() |
16
DeweyReed 2 天前
我的 Crahlytics 中全是这个。试过很多方案都无法根治。
Context.startForegroundService 启动一个 Service ,然后在 Service 中要调用 startForeground ,这是一个从后台启动前台服务的一种方法。没在规定时间内调用 startForeground ,就会触发这种崩溃。这种崩溃几乎必定会有用户遇到。 逆向自己的应用,查找 startForegroundService ,可以定位到哪个 Library 触发的。 |
17
a1210968738 2 天前 via Android
可以 hook 对 ams 的 binder 调用,这样可以拿到发起启动服务的具体堆栈和参数,可以排查出是否有三方库以你意想不到的方式启动前台服务;或者 app 确实没有主动启动前台服务。
|
![]() |
18
joywan 1 天前
启动后台服务必须同时启动前台服务,而你没有启动。
|
![]() |
19
yafoo OP @nightlight9 MQTTService 是我自己写的,我写的启动方式是后台启动
|
![]() |
20
yafoo OP @rumengzhenxing 你这段代码是用来启动前台服务的,我用的是后台服务
|
![]() |
21
yafoo OP @winterbells MQTTService 是我自己写的后台服务
|
![]() |
22
yafoo OP @debuggeeker APP 目前用户不多,支持安卓版本最低安卓 7 ,最高安卓 15 ,目前都没问题,最高安卓 15 正常运行没问题的。
|
![]() |
24
yafoo OP @Bichat 客户端用的 https://github.com/eclipse-paho/paho.mqtt.java 这个,开源的,他自身不会创建前台 service
|
![]() |
25
yafoo OP @a1210968738 感谢回复,这个有点高级,我不会操作,留以后再研究吧
|
27
nightlight9 1 天前
@yafoo #19 14 楼说的靠谱。你把 targetapi 降低到 25 ,就不会报这个错了
|
29
okakuyang 1 天前
安卓早就不是当年那个安卓了,现在必须开启前台服务,并且弹一条通知。
|
![]() |
30
wobuhuicode 1 天前
额,现在的人都不看日志的……这么明显的提示,都不看的
|
![]() |
32
yafoo OP @wobuhuicode 请大佬指教,这个是什么问题。
App 目前用户不多,不过从安卓 7 到安卓 15 ,都有用户在用,我自己也用了 2 年了,没问题。只有这一个用户报这个错 |
![]() |
33
lemos1235 17 小时 40 分钟前
需要启动前台服务。之前遇到过
|