How to set an alarm to be scheduled at an exact time after all the newest restrictions on Android?

后端 未结 6 558
半阙折子戏
半阙折子戏 2020-12-13 13:49

Note: I tried various solutions that are written about here on StackOverflow (example here). Please do not close this without checking if your solution from what you\'ve fou

6条回答
  •  感情败类
    2020-12-13 14:12

    I am the author of the open source project you have mentioned in your question (simple alarm clock).

    I am surprised that using AlarmManager.setAlarmClock did not work for you, because my app does exactly that. The code is in the file AlarmSetter.kt. Here is a snippet:

      val pendingAlarm = Intent(ACTION_FIRED)
                    .apply {
                        setClass(mContext, AlarmsReceiver::class.java)
                        putExtra(EXTRA_ID, id)
                        putExtra(EXTRA_TYPE, typeName)
                    }
                    .let { PendingIntent.getBroadcast(mContext, pendingAlarmRequestCode, it, PendingIntent.FLAG_UPDATE_CURRENT) }
    
                val pendingShowList = PendingIntent.getActivity(
                        mContext,
                        100500,
                        Intent(mContext, AlarmsListActivity::class.java),
                        PendingIntent.FLAG_UPDATE_CURRENT
                )
    
                am.setAlarmClock(AlarmManager.AlarmClockInfo(calendar.timeInMillis, pendingShowList), pendingAlarm)
    

    Basically it is nothing special, just make sure that intent has an action and a target class, which is a broadcast receiver in my case.

提交回复
热议问题