Android 在悬挂中执行功能

Android 在悬挂中执行功能,android,firebase,kotlin,google-cloud-firestore,Android,Firebase,Kotlin,Google Cloud Firestore,我想对Pending帐篷中的Firebase Firestore执行异步调用: MyService.kt override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { /** Intent */ val intent = Intent(this, MainActivity::class.java) val pendingIntent = PendingIntent.getAct

我想对Pending帐篷中的Firebase Firestore执行异步调用:

MyService.kt

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

    /** Intent */
    val intent = Intent(this, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
    val action = NotificationCompat.Action(R.drawable.cancel, "Cancel", pendingIntent)

    val notif = NotificationCompat.Builder(this, channelId).apply {
        setSmallIcon(R.drawable.logo)
        setContentTitle(title)
        setPriority(NotificationCompat.PRIORITY_LOW)
        setColor(ContextCompat.getColor(this@MyService, R.color.colorPrimaryDark))
        setColorized(true)
        addAction(action)
    }.build()

    startForeground(1, notif)

    return START_NOT_STICKY
}
在我的前台服务上单击“操作”按钮时,我希望执行以下操作:

val db = FirebaseFirestore.getInstance()
db.collection("objs").document(id).delete()
而不是去我的主要活动。我希望调用在后台执行,而不是在单击操作时打开我的应用程序


这可能吗?

您可以使用BroadcastReceiver执行操作

    val intent = Intent(this, YourBroadCastReceiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(
        this,
        0,
        intent, 
        0
    )