[英]Unable to start a foreground service using kotlin multiplatform
So i am trying to start a simple foreground service, but seems like i am doing something wrong, or maybe there is a different way to do it using fragment.所以我正在尝试启动一个简单的前台服务,但似乎我做错了什么,或者也许有一种不同的方法可以使用片段来做到这一点。
So this is my service:所以这是我的服务:
class LocationForegroundService : Service() {
companion object {
private const val TRACKING_CHANNEL = "tracking_channel"
private const val TRACKING_NOTIFICATION_ID = 1
fun startService(context: Context, message: String) {
val startIntent = Intent(context, LocationForegroundService::class.java)
startIntent.putExtra("inputExtra", message)
ContextCompat.startForegroundService(context, startIntent)
}
fun stopService(context: Context) {
val stopIntent = Intent(context, LocationForegroundService::class.java)
context.stopService(stopIntent)
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val input = intent?.getStringExtra("inputExtra")
createNotificationChannel()
val notificationIntent = Intent(this, RiderManagementActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
0,
notificationIntent,
0
)
val notification = NotificationCompat.Builder(this, TRACKING_CHANNEL)
.setContentTitle("Test")
.setContentText(input)
.setContentIntent(pendingIntent)
.build()
startForeground(TRACKING_NOTIFICATION_ID, notification)
return START_STICKY
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(
TRACKING_CHANNEL,
"Foreground Location Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(notificationChannel)
}
}
override fun onBind(intent: Intent?): IBinder? = null
}
and this is how a call the service in the fragment:这就是在片段中调用服务的方式:
binding.btnPlayPause.setOnClickListener {
val intent = Intent(requireContext(), LocationForegroundService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requireContext().showShortToast("track")
LocationForegroundService.startService(requireContext(), "Testing")
} else {
requireActivity().startService(intent)
}
}
and i had declared on manifest file:我已经在清单文件中声明:
...
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
...
<application
.../>
<service
android:name=".util.LocationForegroundService"
/>
So i must be missing something because i am getting this error:所以我一定错过了一些东西,因为我收到了这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.quicktendr.mgmt.androidApp, PID: 12693
android.app.RemoteServiceException: Bad notification for startForeground
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1872)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Updating: I created 2 others projects and tested the same service.更新:我创建了另外 2 个项目并测试了相同的服务。 1 android native and the other in kotlin multiplataform like the original, and on the android one worked fine, but the kotlin multiplataform i get the same error.
1 android native and the other in kotlin multiplataform like the original, and on the android one worked fine, but the kotlin multiplataform i get the same error.
As far as I am aware, you need to create your notification channels before you attempt to start a foreground service (>= API 27).据我所知,您需要在尝试启动前台服务之前创建通知通道(>= API 27)。
So trying to create your channels within your service won't work.所以试图在你的服务中创建你的频道是行不通的。
I solved this error adding an icon to the notification, but it is weird just crash on a kotlin multiplatform project but no on android project.我解决了这个错误,在通知中添加了一个图标,但在 kotlin 多平台项目上崩溃但在 android 项目上没有崩溃,这很奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.