2011-01-31 54 views
1

我在ItemEdit Activity的應用程序中創建鬧鐘。它可以在其中編輯/查看他們的筆記/待辦物品的地方,還可以爲該物品設置提醒/警報。我把下面的代碼報警:當我在設置它的活動中時,僅觸發鬧鐘

private void createAlarm() { 
    Intent intent = new Intent(this, ReminderReceiver.class); 
    intent.putExtra("reminder_message", "Reminder Received!"); 
    intent.putExtra("item_id", mRowId); 
    PendingIntent sender = 
     PendingIntent.getBroadcast(
       getApplicationContext(), 
       ALARM_ID, 
       intent, 
       PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 

    // Get the AlarmManager service 
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
    // Set alarm to the time given by the user. 
    am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender); 
} 

這裏是接收

public class ReminderReceiver extends BroadcastReceiver { 

    private static final String TAG = "MyApp"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     try { 
      Bundle bundle = intent.getExtras(); 
      String message = bundle.getString("reminder_message"); 
      Log.v(TAG, message); 
     } catch(Exception e) { 
      Log.v(TAG, "OH SNAP!"); 
      e.printStackTrace(); 
     } 

    } 

編輯:另外我已經在我的清單如下:

<receiver android:process=":remote" android:name="ReminderReceiver"></receiver>

如果我留在Activity,我設置收到的警報罰款。如果我點擊返回按鈕返回到我的ListActivity列出所有項目或完全離開應用程序,警報從不觸發。我是否在設置鬧鐘時做了錯誤,它只是從設置它的活動中觸發的?

謝謝。

+0

我在考慮這個。也許我誤解了一個報警的流程是如何工作的,但是我認爲'AlarmManager`是操作系統範圍廣的,並且無論'Activity'當前處於活動狀態都會觸發。儘管如此,我可能錯了。 – 2011-01-31 20:30:38

回答

1

您需要查看Service而不是Activity,以瞭解不與用戶交互的長時間過程(如鬧鐘)。