【问题标题】:Starting service with an initial value以初始值启动服务
【发布时间】:2011-02-03 15:28:43
【问题描述】:

我正在尝试在我的笔记/待办事项应用程序中使用提醒/警报服务`。我可以为特定项目设置提醒,警报触发并成功显示通知。

我的问题是我的Service 怎么知道哪个笔记/待办事项设置了那个特定的提醒。我希望用户能够单击状态栏中的通知并让触发它的项目出现。但我无法将该信息传递给Service,因为他们不接受来自PendingIntentBundles

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

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,这样我的服务就可以在单击通知时使用相同的_id 启动项目。

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

谢谢!

【问题讨论】:

    标签: android service alarmmanager android-pendingintent


    【解决方案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);
    

    【讨论】:

    • 但这对Service 有效吗?注意我使用的是PendingIntent.getService()。当我尝试在我的服务中从onCreate() 调用getIntent() 时,它未定义:(
    • 哦,快点,@Droidln.net!你让我思考...首先我用Activity 尝试它,提醒/通知正在工作,但它出现了一个空白Activity。然后我用BroadcastReceiver 尝试了它,我想我让它按照我想要的方式工作了!非常感谢让我在 Service 方法之外思考。
    • 绝对是 BroadcastReceiver,永远不需要空白活动 :) 很高兴为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多