工作经理用光了我设置他的时间

时间:2019-02-07 22:49:51

标签: android android-studio android-workmanager

我正在使用workmanager每15分钟发送一次通知。这是我的代码:

jsonify

在MainActivity中:

class UpdateWorker(context : Context, params : WorkerParameters)
    : Worker(context, params) {

    override fun doWork(): Result {

        // Do the work here--in this case, compress the stored images.
        // In this example no parameters are passed; the task is
        // assumed to be "compress the whole library."

        sendNotification("WorkManager", "EL work manager funciona.")

        // Indicate success or failure with your return value:
        return Result.success()

        // (Returning Result.retry() tells WorkManager to try this task again
        // later; Result.failure() says not to try again.)

    }

    fun sendNotification(title: String, message: String) {

        Log.e("Work manager", "Sync");

        val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        //If on Oreo then notification required a notification channel.
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val channel = NotificationChannel("default", "Default", NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(channel)
        }
        val notification = NotificationCompat.Builder(applicationContext, "default")
                .setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.mipmap.ic_launcher)
        notificationManager.notify(1, notification.build())
    }

}

发生的事情是我不明白为什么,如果我指定每15分钟发送一次通知,则该通知当时不会显示。查看显示时间的日志:

//work manager
    val myConstraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            // Many other constraints are available, see the
            // Constraints.Builder reference
            .build()

    val updateWork = PeriodicWorkRequest.Builder(UpdateWorker::class.java, 15, TimeUnit.MINUTES)
            .setConstraints(myConstraints)
            .build()

    WorkManager.getInstance().enqueue(updateWork!!)
//end workmanager

0 个答案:

没有答案