简体   繁体   English

即使应用程序已经销毁,如何使服务在后台运行?

[英]How to make a service that running in the background even if the app's already destroyed?

I want to make a feature to notify user when there's data changes in my firebase database.我想制作一个功能,当我的 firebase 数据库中有数据更改时通知用户。 The problem is when the app is destroyed, then the service automatically destroyed.问题是当应用程序被销毁时,服务会自动销毁。 After that when the data in database change, my app won't notify the user.之后,当数据库中的数据发生变化时,我的应用程序不会通知用户。

Here's some snippet code for my apps.这是我的应用程序的一些片段代码。

Service :服务 :

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.d(TAG, "DogNotificationService created")

        val phone = intent?.extras?.getString("PHONE")

        if(isFirstTime) {
            val firebaseDatabase = FirebaseDatabase.getInstance()

            notificationReference = firebaseDatabase.getReference("walker/$phone/notification")

            val coroutineScope = CoroutineScope(Job() + Dispatchers.IO)

            coroutineScope.launch {
                launchListener()
            }

            isFirstTime = false
        }

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

    fun launchListener() {
        valueEventListener =
            notificationReference.addValueEventListener(object : ValueEventListener {
                override fun onCancelled(p0: DatabaseError) {
                    Log.w(TAG, "Read failed: " + p0.message)
                }

                override fun onDataChange(p0: DataSnapshot) {
                    val notificationData = p0.getValue(String::class.java)

                    if(count > 0) {
                        Log.d(TAG, notificationData)
                        sendNotification(notificationData!!)
                    }

                    count++
                }

            })
    }

When I start the service :当我启动服务时:

private fun startNotificationService() {
        val intent = Intent(context, DogNotificationService::class.java)
        intent.putExtra("PHONE", "081293312313")

        Log.d(TAG, "Start notification service")

        activity?.startService(intent)
    }

If any idea to do this approach, please help.如果有任何想法来做这种方法,请帮忙。

I was working in an app to keep track of the users to allow them record their tracks and found out that services can be killed at any moment if the android system requires free memory.我在一个应用程序中工作以跟踪用户以允许他们记录他们的轨迹,并发现如果 android 系统需要可用内存,则可以随时终止服务。 Even if your service haswakelocks or is running in foreground.即使您的服务有唤醒锁或在前台运行。

The solution I found was to use alarms with a foreground service, if you schedule alarms this alarms will be fired whether your app is still executing or not.我找到的解决方案是将 警报与前台服务一起使用,如果您安排警报,无论您的应用程序是否仍在执行,都会触发此警报。 That way my app could get the device position even though the system had killed the app due to lack of resources.这样,即使系统由于缺乏资源而终止了应用程序,我的应用程序也可以获得设备位置。 It's the only solution I found that works in this scenario.这是我发现在这种情况下有效的唯一解决方案。 An alarm that wakes up the service.唤醒服务的闹钟。

The idea came to me in some google i/o when they said that if you really need your app to continue no matter what you should use alarms instead of services.这个想法是在一些 google i/o 中想到的,当时他们说如果你真的需要你的应用程序继续,无论你应该使用警报而不是服务。

Besides that, if you need the app to be awaked constantly then use exact alarms as the inexact ones in some devices they might be fired 5 minutes later if the time that they should be fired is too near to the current time.除此之外,如果您需要不断唤醒应用程序,那么使用exact警报作为某些设备中的inexact警报,如果它们应该被触发的时间太接近当前时间,它们可能会在 5 分钟后被触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使应用程序被强制停止,也要重新启动服务,即使关闭应用程序,也要在后台继续运行服务如何? - Restart the service even if app is force-stopped and Keep running service in background even after closing the app How? 即使应用程序被强制停止,也要重新启动服务;关闭应用程序后,仍要在后台运行服务。 - Restart the service even if app is force-stopped and Keep running service in background even after closing the app How? 如何使asynctask在后台运行,甚至活动被破坏? - how to make the asynctask run in background even the activity destroyed? 即使活动被破坏,如何使服务保持在内存中? - How to make a service sticky in memory even if activity is destroyed? 如何让应用程序的广播接收器在没有服务在后台运行的情况下继续收听 - How to make the app's broadcast receiver keep listening without having a service running in the background 即使应用被杀死/破坏,如何运行意图服务 - How to run intent service even app is killed/destroyed 即使没有活动运行,如何使服务运行? - How to make service running even if there is no activity running? 如何使服务在后台运行并永不停止? - How to make a service running in background and never stop? 当应用程序被破坏或进入后台时,Android 位置服务停止工作 - Android location service stop working when app destroyed or goes in background 当应用程序已经在后台运行时如何跳过启动画面 - How to skip splashscreen when app is already running in background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM