android - 当警报管理器启动服务时,如何使用 Intent 更新发送到服务的数据?

标签 android service android-intent alarmmanager

我正在编写一个 Android 应用程序,用户可以在其中选择要观看的几只股票,并在符合预定义的警报条件时收到警报。股票数据被保存到自定义 Parcelable 类“警报”的 5 个对象(每个股票和条件一个对象)。定期数据更新是通过由 AlarmManager 启动的服务完成的。警报对象通过将它们放入 Intent 中传递给服务,该 Intent 放入 AlarmManager 的 PendingIntent 中。

    Intent intent = new Intent(this, UpdateService.class);
    Bundle b = new Bundle();
    saveAlertsToBundle(b);      
    intent.putExtras(b);
    intent.setData(Uri.parse("updateManager"));
    PendingIntent pendIntent = PendingIntent.getService(this,0,intent,0);

    // 1min intervall
    long intervall = DateUtils.MINUTE_IN_MILLIS * 1;
    // time of first start
    long firstStartDelay = DateUtils.SECOND_IN_MILLIS * 30;
    long firstStart = System.currentTimeMillis() + firstStartDelay;

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // kill running
    am.cancel(pendIntent);
    //start new
    am.setRepeating(AlarmManager.RTC_WAKEUP,firstStart,intervall,pendIntent);

我的问题是:

第一次启动服务时,只有一个警报对象传递给服务,一切正常。一旦存在更多警报对象,它们也需要传递给服务,但这不适用于上面的代码。该服务不会收到带有其他警报对象的更新 Intent ,但只会收到只有一个警报对象的初始 Intent 。上面的代码正确地创建了一个包含额外警报对象的 Intent,但它们永远不会访问该服务。

所以我的问题是,如何将更新后的 Intent 传递给已经运行的 AlarmManager。

我已经尝试停止 AlarmManager(//kill running 注释处的行)并重新启动它,但这不起作用。或许是因为在他被创建时没有持有相同的警报对象的 Intent ?我试图通过在 Intent 的数据部分设置一个 uri 来解决这个问题,但这也没有帮助。

感谢您的帮助。

最佳答案

您的问题是 PendingIntent 的工作方式。系统管理一个 PengingIntent 池。当您的代码执行以下操作时:

PendingIntent pendIntent = PendingIntent.getService(this,0,intent,0);

这会导致系统搜索与您传入的参数(在本例中为您的Intent)相匹配的PendingIntent。但是,匹配算法PendingIntent 仅使用比较 Intent 的某些字段来确定它是否是您正在寻找的那个。特别是,它不比较 extras 。所以这意味着在您创建第一个 PendingIntent 之后,对 PendingIntent.getService() 的调用将始终返回相同的 PendingIntent池(而不是创建一个新池,这就是您想要的)。

为了在每次调用时调用 PendingIntent.getService() 创建一个新的 PendingIntent,尝试使传递给调用的参数唯一,像这样:

int requestCode = (int) System.currentTimeMillis(); // Create unique request code
PendingIntent pendIntent = PendingIntent.getService(this, requestCode, intent, 0);

由于每次调用 PendingIntent.getService()requestCode 都会不同,因此这应该可以解决您的问题。

根据以下 OP 的评论进行编辑

您想取消现有警报并使用新数据创建一个新警报。在这种情况下,您不需要使用唯一标识符,因为您只想在池中有一个 PendingIntent。但是,您想为此更改数据。试试这个:

// Create a PendingIntent (or update the existing PendingIntent with new values
PendingIntent pendIntent = PendingIntent.getService(this, 0, intent,
                      PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// cancel any pending alarms
am.cancel(pendIntent);
//start new
am.setRepeating(AlarmManager.RTC_WAKEUP,firstStart,intervall,pendIntent);

关于android - 当警报管理器启动服务时,如何使用 Intent 更新发送到服务的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12570404/

相关文章:

android - 可以删除 recyclerview 中以前的项目吗?

java - 将数据传递给已经具有来自 Activity 的 Intent 过滤器的广播接收器

c++ - 启动服务时报错5

powershell - Powershell脚本无法识别我的功能

Android 覆盖显式 Intent

android - 如何使用抽屉导航打开新 Activity ?

java - TelephonyManager 结束通话的替代方法

Android - 在 HttpsURLConnection 中传递 OAuth1

android - 圆角内容的自定义布局

c# - 服务没有及时启动 - c# windows 服务