我有一个广播接收器,可以通过一些简单的计算开始服务。它在我测试过的所有设备上都能按预期工作,但是有些设备崩溃了(我没有一个要手动捕获),这是堆栈跟踪:
java.lang.RuntimeException:
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3045)
at android.app.ActivityThread.access$2200 (ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1454)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5551)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:620)
Caused by: java.lang.IllegalArgumentException:
at com.mypackage.name.tools.WorkerService.onStartCommand (WorkerService.kt)
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3028)
这是服务的方法:
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
println("start command")
init()
return super.onStartCommand(intent, flags, startId)
}
关于问题可能出在哪里的任何想法?我还在onCreate中启动了前台服务,但仅对某些设备有所帮助。
override fun onCreate() {
super.onCreate()
startForegroundInside()
}
private fun startForegroundInside() {
if (App.isOreo()) {
val notificationBuilder = NotificationCompat.Builder(this, createNotificationChannel())
startForeground(101, notificationBuilder.build())
} else {
startForeground(101, Notification())
}
}
private fun init() {
//calc
//...
restartAlarm()
stopSelf()
}