2013-07-12 76 views
0

我正在創建具有鬧鐘功能的鬧鐘應用程序。時間顯示正常,我也正確設置了多個鬧鐘。Android中的鬧鐘設置關閉和打開

我使用不同的ID創建多個警報,並將其保存到數據庫中,以便我可以在列表視圖中查看警報列表。現在我正在嘗試爲我的鬧鐘設置開啓和關閉功能。那裏有問題。

在itemClick在如果報警後它將切斷的幫助下:

Intent intent = new Intent(Main.this,TaskRecieverForAlarm.class); 
    PendingIntent pi = PendingIntent.getBroadcast(Main.this, Integer.parseInt(cont[0]), intent, 0); 
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
    am.cancel(pi); 

上面的代碼取消報警完美的罰款。

要打開我使用的報警:一旦

Intent intent = new Intent(Main.this, TaskRecieverForAlarm.class); 
intent.putExtra("AlarmDate", cont[1]); 
intent.putExtra("key", Integer.parseInt(cont[0])); 
PendingIntent sender = PendingIntent.getBroadcast(Main.this, Integer.parseInt(cont[0]) , intent, PendingIntent.FLAG_UPDATE_CURRENT); 

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 

if(type.equalsIgnoreCase("daily")) 
{ 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); 
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1440*60000 ,sender); 
} 
else if(type.equalsIgnoreCase("weekly")) 
{ 
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);     am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 7*1440*60000 ,sender); 
} 

現在,當我點擊OFF到ON,報警觸發和調用TASKReceiverFORAlarm(廣播接收器),即使報警時間爲4或5距離當前時間的小時。我不確定我哪裏出錯了?

有人可以幫我嗎?

謝謝!

+0

我認爲你需要使用'FLAG_CANCEL_CURRENT',而不是'FLAG_UPDATE_CURRENT',但這只是一個猜測。請嘗試一下,並在這裏回顧結果。 – g00dy

+0

改變了它,但仍然一樣。 PendingIntent sender = PendingIntent.getBroadcast(Main.this,Integer.parseInt(cont [0]),intent,PendingIntent.FLAG_UPDATE_CURRENT); TO PendingIntent sender = PendingIntent.getBroadcast(Main.this,Integer.parseInt(cont [0]),intent,PendingIntent.FLAG_CANCEL_CURRENT); – Mary

+0

好的,這不起作用,請嘗試下面的解決方案。 – g00dy

回答

0

我想我找到了答案here

public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) 

Added in API level 1 
Schedule a repeating alarm. Note: for timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler. If there is already an alarm scheduled for the same IntentSender, it will first be canceled. 

Like set(int, long, PendingIntent), except you can also supply a rate at which the alarm will repeat. This alarm continues repeating until explicitly removed with cancel(PendingIntent). If the time occurs in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval. 

If an alarm is delayed (by system sleep, for example, for non _WAKEUP alarm types), a skipped repeat will be delivered as soon as possible. After that, future alarms will be delivered according to the original schedule; they do not drift over time. For example, if you have set a recurring alarm for the top of every hour but the phone was asleep from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens, then the next alarm will be sent at 9:00. 

If your application wants to allow the delivery times to drift in order to guarantee that at least a certain time interval always elapses between alarms, then the approach to take is to use one-time alarms, scheduling the next one yourself when handling each alarm delivery. 

Parameters 
type One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP}, RTC or RTC_WAKEUP. 
triggerAtMillis time in milliseconds that the alarm should first go off, using the appropriate clock (depending on the alarm type). 
intervalMillis interval in milliseconds between subsequent repeats of the alarm. 
operation Action to perform when the alarm goes off; typically comes from IntentSender.getBroadcast(). 

您使用該功能的方法是:

am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1440*60000 ,sender); 

試試這個:

//The only variable here is the desired hour of the alarm, which 
// has to be obtained in milliseconds 
long alarmSetAt = // The hour of the Alarm for the current date in milliseconds 
long time = cal.getTimeInMillis() - alarmSetAt; 

if(time > 0){ 

    time = -time + cal.getTimeInMillis(); 
} 
else{ 
    time = time + cal.getTimeInMillis() + 1440*60000; 
} 

am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1440*60000 ,sender);