如何设置alarmManager以显示特定时间的通知

时间:2016-09-19 12:42:56

标签: android alarmmanager android-notifications

我正在开发一个应用程序,其中我找到当前日期和时间并计算7天后的日期和时间。我获得了成功,但我想在我计算的那个日期设置通知。我怎么能这样做

找出当前日期和时间以及结束日期和时间的代码: -

private void endTime(){
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MINUTE, 500);

    endTime = String.valueOf(c.get(Calendar.MONTH) + 1) + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "/" +
            String.valueOf(c.get(Calendar.YEAR)) + " " + String.valueOf(c.get(Calendar.HOUR_OF_DAY)) + ":" +
            String.valueOf(c.get(Calendar.MINUTE)) + ":" + String.valueOf(c.get(Calendar.SECOND));
    Log.e(TAG,"End Time::"+endTime);
}

private void currentTime() {

    Calendar c = Calendar.getInstance();
    currentTime = String.valueOf(c.get(Calendar.MONTH) + 1) + "/" + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) + "/" +
            String.valueOf(c.get(Calendar.YEAR)) + " " + String.valueOf(c.get(Calendar.HOUR_OF_DAY)) + ":" +
            String.valueOf(c.get(Calendar.MINUTE)) + ":" + String.valueOf(c.get(Calendar.SECOND));
    Log.e(TAG,"Current Time::"+currentTime);


}

1 个答案:

答案 0 :(得分:0)

创建一个服务类,使用IntentService扩展以在后台运行

 Intent intentToQB = new Intent(YourActivity.this, BackgroundService.class);

  PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),1212, intentToQB, PendingIntent.FLAG_ONE_SHOT);
  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),5*60*1000, pendingIntent);