如何基于本地xml文件时间值创建android通知?
我如何在android应用程序上实现通知功能,在xml上的设置时间,该应用程序将显示通知 假设一个本地xml文件的值为notifyat=5:00pm,那么我希望应用程序每天都显示一个基于该值的通知 我一直在阅读本教程如何基于本地xml文件时间值创建android通知?,android,notifications,Android,Notifications,我如何在android应用程序上实现通知功能,在xml上的设置时间,该应用程序将显示通知 假设一个本地xml文件的值为notifyat=5:00pm,那么我希望应用程序每天都显示一个基于该值的通知 我一直在阅读本教程 如何让应用程序每天自动读取xml文件并在指定时间向我显示通知?要执行类似操作,您需要使用AlarmManager。以下是在此特定时间运行代码的示例: // create intent launchIntent = new Intent(this, MyAlarmRe
如何让应用程序每天自动读取xml文件并在指定时间向我显示通知?要执行类似操作,您需要使用AlarmManager。以下是在此特定时间运行代码的示例:
// create intent
launchIntent = new Intent(this, MyAlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, launchIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long interval = TimeUnit.DAYS.toMillis(1); // the interval is one day
long firstTime = 0;
// create a Calendar object to set the real time at which the alarm
// should go off
Calendar alarmTime = Calendar.getInstance();
alarmTime.set(Calendar.HOUR_OF_DAY, 17);
alarmTime.set(Calendar.MINUTE, 0);
alarmTime.set(Calendar.SECOND, 0);
Calendar now = Calendar.getInstance();
// set the alarm for today at 5pm if it is not yet 5pm
if (now.before(alarmTime)) {
firstTime = alarmTime.getTimeInMillis();
} else {
// set the alarm for the next day at 5pm if it is past 5pm
alarmTime.add(Calendar.DATE, 1);
firstTime = alarmTime.getTimeInMillis();
}
// Repeat every day at 5pm
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, interval,
pendingIntent);
“MyAlarmReceiver”类用于放置启动通知的代码。请确保在AndroidManifest中声明您的BroadcastReceiver类。这不是“推送通知”是什么。那么,我希望我的应用程序做什么,我应该看什么呢?我想您没问题,只需知道这些通知称为“通知”。推送通知是服务器发送给客户端的东西,我想这不是您正在做的。不,我不是在使用服务器,而是使用本地数据。非常感谢。因此,在本例中,MyAlarmReceiver将是实际启动通知的类?是的,这是正确的。还有其他方法可以做到这一点,即服务、IntentService,但这是一个可行的选项。您只需将要运行的代码(通知)放入BroadcastReceiver类的onReceive()中。AlarmManager将向运行您的通知的BroadcastReceiver发送信号。