2011-01-31 55 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,以了解不与用户交互的长时间过程(如闹钟)。