2016-03-03 127 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