2016-03-03 128 views
0

我在分析中存储日期和时间,并得到所有数据,但即使是单个闹钟也没有被触发。例如,我将时间存储在数据库中,例如下午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"); 

} 

    }); 

} 

回答

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?

+0

但我做了同样的什么是错在我的代码? –

+0

您需要使用PendingIntent.getBroadcast,它将为待定意图采用不同的广播ID。 PendingIntent pendingIntent = PendingIntent.getBroadcast(context,i,intent,0); – Naresh