- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经在 StackOverflow 和 Web 中阅读了许多相同的问题,但即使从任务中删除应用程序(强制终止),也无法配置“永无止境的服务”或运行之类的内容。我只是想知道像 Viber 或 WhatsUp 这样的服务是如何工作的,因为当我们强制终止这些应用程序时,当有人写信给我们时,我们仍然能够接收消息(所以服务仍在运行)。我知道前台服务,但这不是解决方案,因为用户不想看到通知。所以这里是我尝试过的。 :此代码在服务内部运行以检测实时更改,只是希望它在应用程序的每个条件下保持活跃:前景、背景、已删除等...
firestoreDb!!.collection("example").document("example").collection("real_time_request")
.addSnapshotListener { documentSnapshot: QuerySnapshot?, _: FirebaseFirestoreException? ->
}
当用户的集合发生变化时,我正在使用这样的服务从 Firestore 数据库获取实时数据。
我尝试服务的方式是:
FirestoreListeners : IntentService("Firestore Listeners")
FirestoreListeners : Service
FirestoreListeners : JobIntentService
当应用程序处于前台或后台时,上述一切工作正常,但在应用程序被强制关闭后服务被终止(从任务中删除时)
我已尝试在 list 中进行此更改:
android:stopWithTask="false"
android:directBootAware="true"
android:process=":remote"
在应用程序层次结构中:
android:persistent="true"
在 IntentService onHandleIntent 中:
setIntentRedelivery(true)
在服务 onStartCommand :
return START_STICKY
如果系统破坏或杀死它,尝试重新启动服务:
override fun onTaskRemoved(rootIntent: Intent) {
val restartServiceIntent = Intent(applicationContext, this::class.java)
restartServiceIntent.setPackage(packageName)
val restartServicePendingIntent = PendingIntent.getService(applicationContext, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT)
val alarmService = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent)
Log.e("Service Firestore ", "Task Removed")
super.onTaskRemoved(rootIntent)
}
但没有什么可以正常工作。实现这一目标的最佳解决方案是什么?因此,目的是让某些东西在后台运行并监听 Firestore 更改(或其他东西),即使在应用程序被杀死或从任务中删除之后,比如 Viber 等......也许他们正在使用没有通知图标的前台服务?但我不认为Android允许我们做这种前台服务(没有通知)
我已经阅读了一些关于 WorkManager
的文章,正如 Google 所说:
Note: WorkManager is intended for tasks that require a guarantee that the system will run them even if the app exits, like uploading app data to a server. It is not intended for in-process background work that can safely be terminated if the app process goes away; for situations like that, we recommend using ThreadPools.
但无法弄清楚它是如何工作的或如何用于我的目的
谢谢
最佳答案
当您的应用处于未运行/前台时,您可以使用 FCM 推送通知来唤醒设备。当应用收到推送通知时,您可以启动您的服务来执行所需的任务。同样,当应用程序处于后台时,您可能无法启动服务,因为您可能需要将服务作为前台服务运行。
关于java - Android - Service、IntentService、JobIntentService - 如果应用程序被杀死,它们就会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873643/
JobIntentService类似乎已被弃用。然而,Android documentation for services说:“您可以使用 JobIntentService 作为与较新版本的 Andr
在 com.example.project.packageA 包中,我有一个扩展 JobIntentService 的类,定义如下: public class MyService extends Jo
我有一个监听电源连接事件的广播接收器。每当设备连接电源时,我都会尝试将文件从 APP 传输到运行 Ubuntu 的机器中的服务器。文件通过蓝牙传输。由于文件传输很重要,如果出于任何原因传输出现错误,或
如何取消 JobIntentService ?我在文档中看不到这样做的方法,除了 JobSheduler API 和 Context#stopService(...) 方法,我不确定它是否是正确的方法
我的问题很简单,但我已经为此苦苦挣扎了很长时间! 我有一个调用 onBound() 方法的绑定(bind)服务: @Override public IBinder onBind(Intent
我尝试在 Android 上使用 JobIntentService,但在访问 onHandleWork 方法内的类字段时遇到问题。我的 discoverable 字段在 onHandleWork 方法
我想在我的应用中开发文本转语音 (TTS) 功能。它必须在 Service 中实现,因为用户可能会离开“详细信息”Activity 并返回“主要”Activity 甚至离开我的应用程序,它仍然应该大声
所以我目前使用 JobScheduler 根据各种条件安排作业,我想我会想使用 JobIntentService 来执行它们。但是,我看到 JobIntentService 也有一个 enqueueW
我一直在研究如何安排针对设备 Oreo + 的警报。我目前的方法是使用 AlarmManager,一种铃声服务和广播接收器。我让闹钟响起并显示通知,但通知和闹钟不会为 Oreo + 设备触发。 由于
我正在开发一款支持 Android API 版本 21 及更高版本的应用。我的大部分后台任务都是使用 API 21 中引入的 JobScheduler 设计的。 我最近遇到了 API 26 中引入的
我最近正在更新一个我正在使用 JobIntentService 而不是常规 IntentService 处理推送通知的应用程序,因为这似乎是处理此问题的正确方法在 Lollipop 之前的设备以及后期
来自安卓开发者JobIntentService在 Android O 或更高版本上运行时,工作将通过 JobScheduler.enqueue 作为作业分派(dispatch)。在旧版本平台上运行时,
Google 最近弃用了 IntentService:https://android.googlesource.com/platform/frameworks/base.git/+/6f8b09029
关注 FetchAddressIntentService使用 IntentService 实现(在 Kotlin ): class FetchAddressIntentService //Const
我不明白这两个 API 有什么区别。我的意思是什么时候使用第一个。为什么会有 JobIntentService ? 提前致谢 最佳答案 我建议阅读这篇文章,解释 intent service and
我需要在我的应用程序运行时和启动时处理通过 AlarmManager 发布的 Intent 。我已经编写了 JobIntentService 的子类来处理 Intent ,但它没有按预期工作:没有调用
当使用 JobIntentService 时,我在使用 targetSDK 26 的 Android 8 上遇到了 SecurityException 崩溃。 这是来自 JobIntentServic
我目前正在从 Service 类扩展我的自定义服务实现。作为 Android-O 迁移的一部分,我想使用 JobIntentService。 目前我所有的逻辑都在服务的 onStartCommand
我正在运行 JobIntentService 以在后台执行任务。使用 JobIntentService 的原因是这样用户可以在操作发生时最小化屏幕,即使 Android 操作系统破坏了 Activit
我有一个 JobIntentService,它会在每次推送通知进入时启动,告诉服务去获取更多数据。当应用程序在前台运行时,一切正常。 当应用程序在后台并且收到多个推送通知时, Intent 会排队并多
我是一名优秀的程序员,十分优秀!