使用初始值启动服务

时间:2011-02-03 15:28:44

标签: android service alarmmanager android-pendingintent

我正在尝试在我的note / todo应用程序中使用提醒/警报服务。我能够为特定项目设置提醒,并且警报触发并成功显示通知。

我的问题是我的Service如何知道哪个音符/待办事项设置了该特定提醒。我希望用户能够点击状态栏中的通知并让触发它的项目出现。但我无法将这些信息传递给Service,因为他们不接受来自Bundles的{​​{1}}。

我目前使用以下内容设置闹钟:

PendingIntent

我只需要一种方法来发送数据库中项目的private void createAlarm() { Intent i = new Intent(this, AlarmService.class); PendingIntent sender = PendingIntent.getService(this, 0, i, 0); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender); } ,这样我的服务就可以在点击通知时启动具有相同_id的项目。

我希望我的问题不会太混乱。

谢谢!

1 个答案:

答案 0 :(得分:1)

为什么不把所有需要都放在Intent数据中?像这样:

    final Intent intent = new Intent(context, UpdatesActivity.class);
    intent.putExtra(ID, "foo");
    final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

然后在接收端你做

    String id = getIntent().getStringExtra(ID);