【问题标题】:Android startForegroundService is not runningAndroid startForegroundService 未运行
【发布时间】:2022-06-10 23:19:53
【问题描述】:

我想在android应用程序的后台运行一个应用程序,但是它看到了文件(我在调试中观察到了),但是它没有进入文件并运行代码,我无法解决。有什么想法吗?

类 MyService : Service() {

override fun onBind(intent: Intent): IBinder {
    TODO("Return the communication channel to the service.")
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    
    Toast.makeText(applicationContext,"Test",Toast.LENGTH_SHORT).show()

    var serviceId = "Foreground Servie ID3"
    var notificationChannel =
        NotificationChannel(serviceId, serviceId, NotificationManager.IMPORTANCE_LOW)
    getSystemService(NotificationManager::class.java).createNotificationChannel(
        notificationChannel
    )
    var notification = Notification.Builder(applicationContext, serviceId)
        .setContentText("Service is running ..")
        .setContentTitle("Service is enabled !")
        .setSmallIcon(R.drawable.account)
    startForeground(1001, notification.build())

    return super.onStartCommand(intent, flags, startId)
}

} -- 运行服务 Kt 文件

 fun foregroundServiceRunning(): Boolean {
    val manager = requireContext().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager?
    if (manager != null) {
        for (service in manager.getRunningServices(Integer.MAX_VALUE)) {
            if (MyService().javaClass.name.equals(service.service.className)) {
                println("Servis calısıyor")
                return true
            }
        }
        return false
    }
    return false
}

【问题讨论】:

  • 您记得在清单中为它添加<service> 元素吗?

标签: android


【解决方案1】:

感谢@Mike M.sayende çözdüm,ufak seyler çok büyük sorunlar olusturabiliyor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2021-01-13
    • 1970-01-01
    相关资源
    最近更新 更多