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列出所有项目的位置或完全离开应用程序,则警报永远不会触发。我在设置警报时是否做错了什么,它只从设置它的活动触发?

谢谢。

4

1 回答 1

1

您需要查看服务而不是Activity不与用户交互的长期进程(如闹钟)。

于 2011-02-03T14:07:20.970 回答