多重警报未设置

时间:2016-03-03 10:20:52

标签: android

我在解析时存储日期和时间并获取所有数据,但即使是单个警报也未触发。例如,我将时间存储在数据库中,如下午2点,下午3点,下午4点等,以及它们的日期,但不会触发警报。 请帮助我..

这是我的主要课程

   ArrayList<PendingIntent> intentArray; 

  AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);

 intentArray = new ArrayList<PendingIntent>();



 Calendar calendar=Calendar.getInstance();

   ParseQuery<ParseObject> query = ParseQuery.getQuery("Description");

   query.whereEqualTo("user", user);

   query.findInBackground(new FindCallback<ParseObject>() {

 @Override

 public void done(List<ParseObject> list, ParseException e)                     
if (e == null) {
int count = 0;
     for (ParseObject obj : list) {

    calendar.set(Calendar.YEAR, obj.getInt("Year"));
    calendar.set(Calendar.MONTH, obj.getInt("Month"));
    calendar.set(Calendar.DAY_OF_MONTH, obj.getInt("Day"));
   calendar.set(Calendar.HOUR_OF_DAY, obj.getInt("Hour"));
   calendar.set(Calendar.MINUTE, obj.getInt("Miniute"));
  calendar.set(Calendar.SECOND, 0);

   Intent intent = new Intent("net.learn2develop.DisplayNotification");

     intent.putExtra("NotifID", 1);

  PendingIntent  
 pendingIntent=PendingIntent.getActivity(getBaseContext(),ncount++,intent,    
 0);

alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis()
,pendingIntent);

    intentArray.add(pendingIntent);

}//end for loop

  } else {

      Log.d("Test", "Error Occur");

 }  

    });

}

1 个答案:

答案 0 :(得分:0)

如果要设置多个警报(重复或单个),则只需要使用不同的requestCode创建其PendingIntents。如果requestCode相同,则新警报将覆盖旧警报。

以下是创建多个单个警报并将它们保存在ArrayList中的代码。

// context variable contains your `Context`
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

for(i = 0; i < 10; ++i)
{
   Intent intent = new Intent(context, OnAlarmReceiver.class);
   // Loop counter `i` is used as a `requestCode`
   PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
   // Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
   mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                SystemClock.elapsedRealtime() + 60000 * i, 
                pendingIntent); 

   intentArray.add(pendingIntent);
}

您还可以看到问题:How to set more than one alarms at a time in android?