2

Android摸索记录--设置生日提醒

 2 years ago
source link: https://www.daozhao.com/10472.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Android摸索记录--设置生日提醒

如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!

Android摸索记录--设置生日提醒

在上一篇《Android摸索记录--获取android联系人》中,我们已经将自己手机google账号的联系给取出来了。

先把联系人信息需要简单组合下再使用,将同一个人的信息条目都存在一个User下,按如下格式存储。

package com.daozhao.hello

data class User (val key: String, val id: String, val name: String, val phones: ArrayList<UserPhone>, var events: ArrayList<UserEvent>, val note: String?) {

}

class UserPhone (val userKey: String, data: String?, val type: String?, val label: String? ) {
    var data = data?.replace(" ", "")
}

class UserEvent (val userKey: String, data: String?, val type: String?, val label: String?, ) {
    var data = data?.replace(" ", "")?.replace("/", "")?.replace("-", "")
}

其中phones这个list里面就是用户所有的电话号码,统一成xxxxxxxxxxx形式,移除了可能存在空格,events这个list所有的事件(主要是生日)提醒,统一成yyyyMMdd形式,移除了可能存在的空格、/、-这些分隔符。

我们用AlarmManagerCalendar、””PendingIntent来设置定时,等时间到了就给AlarmReceiver通知。

// 利用日历设置闹钟
    fun setCalendarAlarm(user: User, index: Int) {
        user.events.forEach {
            val birthday = it.data;
            if (birthday != null) {
                val year = 2022;
                val month = birthday.substring(4, 6);
                val date = birthday.substring(6, 8);
                val amMgr = requireContext().getSystemService(Context.ALARM_SERVICE) as AlarmManager
                val currentDate = Calendar.getInstance()
                val calendar: Calendar = Calendar.getInstance().apply {
                    set(Calendar.MONTH, month.toInt() - 1) // 从0开始,即1月份为0 12月份为11
                    set(Calendar.DATE, date.toInt())
                    set(Calendar.HOUR_OF_DAY, 9);
                    set(Calendar.MINUTE, 0);
                    if (this.before(currentDate)) {
                        set(Calendar.YEAR, year + 1)
                        Log.i("ALARM_EXPIRES", user.name + ": " + year + month + date)
                    }
                }
                val intent = Intent(mContext, AlarmReceiver::class.java).let { intent ->
                    intent.action = CONST.ALARM_ACTION
                    intent.putExtra("name", user.name)
                    intent.putExtra("birthday", birthday)
                }
                val pendingAlarmIntent = PendingIntent.getBroadcast(mContext, index + birthday.toInt(), intent, PendingIntent.FLAG_IMMUTABLE)

                Log.i("ALARM", user.name + ": " + year + month + date + " size: " + index +  " ts: " + calendar.timeInMillis)
                amMgr.set(
                    AlarmManager.RTC_WAKEUP,
                    calendar.timeInMillis,
                    pendingAlarmIntent
                )
            }
        }
    }

file

图中的武汉xx的日期是1月15号,在当前来看就是过期的,所以需要设置成明年的1月15号再提醒 今天日期就是20220426,我创建了一个测试数据AAtest的生日就是今天。

现在可以对每个User设置闹钟,这里面有几个小点需要注意的。

  1. 月份是从0开始的,也就是说一月份是0,十二月份是11
  2. 过期的事件日期需要处理下,比较下事件时间与当前时间,如果已经过期了,则不用提醒了,需要设置成明年再提醒了

我们在AlarmReceiver中就可以拿到对应的信息,并展示相应的通知信息了,这里我们先简单toast一下。

class AlarmReceiver: BroadcastReceiver() {
    override fun onReceive(mContent: Context?, intent: Intent?) {
        if (CONST.ALARM_ACTION.equals(intent!!.getAction())) {
            val name = intent.getStringExtra("name");
            val text = "Happy birthday " + name;
            Toast.makeText(mContent, text, Toast.LENGTH_LONG).show();
        }
    }
}
  • 把之前JS里面的农历转换加上,因为还是有不少朋友的生日是过农历的,需要将其转完后公历再设置提醒
  • 支持通知栏的展示提醒信息

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK