2011-01-20 69 views
0

我確定這是簡單的事情,但我沒有搞清楚,我試圖做出一個簡單的重複鬧鐘,它從來沒有被觸發,我擁有的是:Android鬧鐘沒有被觸發

private void setupAlarms() 
{ 
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

    Intent intent = new Intent(this, RepeatingAlarm.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid.this, 0, intent, 0); 

    GregorianCalendar fifteenSeconds = (GregorianCalendar)Calendar.getInstance(); 
    fifteenSeconds.add(Calendar.MINUTE, 0); 
    fifteenSeconds.set(Calendar.SECOND, 15); 

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
      SystemClock.elapsedRealtime(), fifteenSeconds.getTimeInMillis(), pendingIntent); 
} 

這是從主要的onCreate調用稱爲

我報警接收機:

public class RepeatingAlarm extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Toast.makeText(context, R.string.hello, Toast.LENGTH_SHORT).show(); 
    } 
} 

在我的表現,我已經加入:

<receiver android:name=".RepeatingAlarm" android:process=":remote" /> 

任何幫助,非常感謝

+0

我不確定如果fifteenSeconds.getTimeInMillis()真的返回15 * 60 * 1000 [毫秒]? – 2011-01-20 13:17:17

+0

我認爲它沒有。您可以將日曆設置爲秒數,但毫秒值在2個相同設置時間內會有所不同。 – r1k0 2011-01-20 13:30:24

回答

0

您所添加的意圖過濾器,以你的廣播接收器? 代碼可能看起來像這樣在你的AndroidManifest.xml文件:

<receiver android:name=".RepeatingAlarm" android:exported="true"> 
     <intent-filter> 
      <action android:name="intent id text" /> 
     </intent-filter> 
    </receiver> 

和創建你的意圖時,做這樣的事情:

Intent intent = new Intent("intent id text") 

這裏的「意圖ID文本」可以是任何字符串你用來確定你的意圖。如果您重新啓動設備,也可以重置Android鬧鐘,這可能是您需要查看的內容。

希望這會有所幫助。