【发布时间】: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