首页 > 解决方案 > Android workmanager 作业在应用程序关闭后不运行(三星 s9+)

问题描述

我正在尝试使用 WorkManager 运行一次性工作。我不知道为什么,但是如果我从最近的应用程序菜单中关闭我的应用程序,直到我启动应用程序才会开始:/。用三星 Galaxy s9+ 试过。安卓 9 派。任何帮助,将不胜感激。据我所知,即使手机重新启动,workmanager 也应该可以工作。

 val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.NOT_REQUIRED).build()
            val request = OneTimeWorkRequestBuilder<NotificationJob>()
                .addTag(item.uniqueId)
                .setInputData(data)
                .setConstraints(constraints)
                    //TODO revert
//                .setInitialDelay(warrantyItem.reminderDate - System.currentTimeMillis(), TimeUnit.MILLISECONDS)
                .setInitialDelay(1, TimeUnit.MINUTES)
                .build()
            WorkManager.getInstance(this@MainActivity).enqueue(request)

工作很简单。只是通知:

 override fun doWork(): Result {
        // Get the input
        val uniqueId = inputData.getString(JOB_KEY)
        if (uniqueId != null) {
            sendNotification(uniqueId, "title", "subtitle")
        }


        return Result.success()
    }

    private fun sendNotification(uniqueId: String, title: String, subtitle: String) {
        val intent = Intent(applicationContext, MainActivity::class.java)
        intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
        intent.putExtra(NOTIFICATION_ID, id)

        val notificationManager =
            applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        val bitmap = applicationContext.vectorToBitmap(R.drawable.ic_warranty_icon)
        val titleNotification = "$title"
        val subtitleNotification = subtitle
        val pendingIntent = getActivity(applicationContext, 0, intent, 0)
        val notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL)
            .setLargeIcon(bitmap).setSmallIcon(R.drawable.ic_warranty_icon)
            .setContentTitle(titleNotification).setContentText(subtitleNotification)
            .setDefaults(DEFAULT_ALL).setContentIntent(pendingIntent).setAutoCancel(true)

        notification.priority = PRIORITY_MAX

        if (SDK_INT >= O) {
            notification.setChannelId(NOTIFICATION_CHANNEL)

            val ringtoneManager = getDefaultUri(TYPE_NOTIFICATION)
            val audioAttributes = AudioAttributes.Builder().setUsage(USAGE_NOTIFICATION_RINGTONE)
                .setContentType(CONTENT_TYPE_SONIFICATION).build()

            val channel =
                NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_NAME, IMPORTANCE_HIGH)

            channel.enableLights(true)
            channel.lightColor = RED
            channel.enableVibration(true)
            channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
            channel.setSound(ringtoneManager, audioAttributes)
            notificationManager.createNotificationChannel(channel)
        }

        with(NotificationManagerCompat.from(applicationContext)) {
            // notificationId is a unique int for each notification that you must define
            notify(uniqueId, ID, notification.build())
        }

    }

标签: androidkotlinandroid-workmanager

解决方案


推荐阅读