【问题标题】:Multiple Instances of Pending Intent待处理意图的多个实例
【发布时间】:2010-09-16 19:52:49
【问题描述】:

我创建了一个小部件,单击该小部件会激活 PendingIntent。问题是当屏幕上有多个小部件时,只有最新的小部件会启动 PendingIntent。

我已经阅读了一些关于唯一请求代码的信息,但没有弄清楚这一点。

有什么想法可以让我拥有多个小部件并且 PendingIntents 可以为每个小部件工作吗?

这是我的代码的 sn-p:

Intent openApp = new Intent(context, RunningTally.class);
    openApp.putExtra("widgetId", appWidgetId);
    PendingIntent pendingAppIntent = 
        PendingIntent.getActivity(context, 0, openApp, PendingIntent.FLAG_CANCEL_CURRENT  );
    views.setOnClickPendingIntent(R.id.openFull, pendingAppIntent);

【问题讨论】:

    标签: android widget


    【解决方案1】:

    碰巧在发布我的问题后,我想出了一个答案。我将 appWidgetId 作为“唯一”请求代码传入,瞧!现在是 sn-p:

    Intent openApp = new Intent(context, RunningTally.class);
        openApp.putExtra("widgetId", appWidgetId);
        PendingIntent pendingAppIntent = 
            PendingIntent.getActivity(context, appWidgetId, openApp, 
                                      PendingIntent.FLAG_CANCEL_CURRENT);
        views.setOnClickPendingIntent(R.id.openFull, pendingAppIntent);
    

    【讨论】:

    • 最糟糕的是文档说:requestCode 发件人的私人请求代码(目前未使用)。所以我看到了,但我没有尝试使用它,因为我认为它不会起作用!
    • 注意 - 它不再被标记为 currently not used developer.android.com/reference/android/app/…
    • 正如我所注意到的,这应该适用于任何类型的待处理意图(包括通知的意图)。我认为最好放置一个管理所有类型的类,以避免覆盖它们。
    • 这里的关键是唯一的请求码。如果你通过两个相同的 - 一个将不起作用。
    • @android-developer 是的,具有不同请求代码的多个待处理意图实例的概念也与AlarmManager 完美配合
    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    相关资源
    最近更新 更多