0

如何在 android 应用程序上实现通知功能,其中在 xml 上找到的设定时间,应用程序将显示通知?

假设一个本地 xml 文件的值是 notifyat=5:00pm,所以我希望应用程序每天都根据该值显示通知?

我一直在阅读本教程http://www.vogella.com/articles/AndroidNotifications/article.html

如何让应用程序每天自动读取 xml 文件并在指定时间向我显示通知?

4

1 回答 1

0

为了做这样的事情,您将需要使用 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 类。

于 2012-06-02T22:03:16.557 回答