AlarmManager如何在点击时打开活动?

时间:2019-03-02 19:19:36

标签: android kotlin alarmmanager android-pendingintent

我正在为此努力第二天,真的需要帮助

这是我开始通知的方式:

object MyAlarmManager  {
//some stuff
private fun createNotification(title: String, calendar: Calendar){

        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent(context, AlertReceiver::class.java)
        val pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0)
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent);
    }
}

alertReceiver:

class AlertReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        val notificationHelper = NotificationHelper(context!!)
        val nb = notificationHelper.channelNotification
        notificationHelper.manager!!.notify(1, nb.build())
    }
}

这是助手

class NotificationHelper(private val base: Context) : ContextWrapper(base) {

    private var mManager: NotificationManager? = null

    val manager: NotificationManager?
        get() {
            if (mManager == null) {
                mManager = base.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            }

            return mManager
        }

    val channelNotification: NotificationCompat.Builder
        get() = NotificationCompat.Builder(applicationContext, channelID)
            .setContentTitle("Alarm!")
            .setContentText("Your AlarmManager is working.")
            .setSmallIcon(R.mipmap.ic_launcher)

    init {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel()
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private fun createChannel() {
        val channel = NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)

        manager!!.createNotificationChannel(channel)
    }

    companion object {
        const val channelID = "channelID"
        const val channelName = "Channel Name"
    }
}

现在,请,我需要更改我的代码吗?注意,我不是从Activity开始的。我试图在这里和那里添加一些东西,特别是val前行的tent。我切换为.getActivity并传​​递了上下文(来自App类),但这没有用

0 个答案:

没有答案