1

我正在努力使通知在我的应用程序中工作。我能够设置一个按钮 onclicklistener 来触发通知,但现在我想在每天下午 3 点设置自动通知。问题是我应该把通知代码和警报管理器代码放在哪里?因为我不再使用按钮触发器了。另外,有没有一种简单的方法让它像服务一样在后台运行,所以即使应用程序没有打开,它也会通过每天在特定时间触发通知来自动提醒用户?谢谢

通知

    Intent intent = new Intent(this, GFXSurface.class);
    PendingIntent pi2 = PendingIntent.getActivity(this, 0, intent, 0);
    String body = "This is a message from the Health Effect";
    String title = "The Health Effect";
    Notification n = new Notification(R.drawable.notification, body,
            System.currentTimeMillis());
    n.setLatestEventInfo(this, title, body, pi2);
    n.defaults = Notification.DEFAULT_ALL;
    nm.notify(uniqueID, n);
    finish();

报警器

    calendar.set(Calendar.HOUR_OF_DAY, 15);
    calendar.set(Calendar.MINUTE,0);
    calendar.set(Calendar.SECOND, 0);
    AlarmManager am = (AlarmManager) this
            .getSystemService(this.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getService(this, 0, new Intent(this,
            StatusBar.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);
4

0 回答 0