android - 同一 Intent 的 Widget 上的多个 OnClick

标签 android widget android-pendingintent

我的小部件启动一个服务,该服务更新 3 个 LinearLayouts 的列表。

我想在每个 LinearLayout 上设置一个具有不同 Extra 的 SetOnClickPeningIntent

但是当我启动小部件并想单击 LinearLayouts 时,只有最后一个是 onclickable =/我不知道出了什么问题。希望你能帮助我。

RemoteViews remoteViews = new RemoteViews(getPackageName(),
        R.layout.widget_layout);


for (int widgetId : appWidgetIds) {


    int[] lls = { R.id.ll_con_1, R.id.ll_con_2, R.id.ll_con_3 };


        for (int i = 0; i < jray.length(); i++) {

                    try {

                        JSONObject o = jray.getJSONObject(i);

                        //Onclick
                            if(i == 0)
                            {
                                Intent msg_intent = new Intent(getApplicationContext(), MSGsOpenMsg.class);
                                msg_intent.putExtra("messageid", o.getString("id"));
                                PendingIntent msg_pendingIntent = PendingIntent.getActivity(
                                        getApplicationContext(), 0, msg_intent, Intent.FLAG_ACTIVITY_NEW_TASK);
                                remoteViews.setOnClickPendingIntent(R.id.ll_con_1, msg_pendingIntent);
                            }
                            else if(i == 1)
                            {           
                                Intent msg_intent1 = new Intent(getApplicationContext(), MSGsOpenMsg.class);
                                msg_intent1.putExtra("messageid", o.getString("id"));
                                PendingIntent msg1_pendingIntent = PendingIntent.getActivity(
                                        getApplicationContext(), 0, msg_intent1, Intent.FLAG_ACTIVITY_NEW_TASK);
                                remoteViews.setOnClickPendingIntent(R.id.ll_con_2, msg1_pendingIntent);
                            }
                            else if(i == 2)
                            {
                                Intent msg_intent = new Intent(getApplicationContext(), MSGsOpenMsg.class);
                                msg_intent.putExtra("messageid", o.getString("id"));
                                PendingIntent msg2_pendingIntent = PendingIntent.getActivity(
                                        getApplicationContext(), 0, msg_intent, Intent.FLAG_ACTIVITY_NEW_TASK);
                                remoteViews.setOnClickPendingIntent(R.id.ll_con_3, msg2_pendingIntent);
                            }

                    } catch (JSONException e) {

                    }
        }

}

最佳答案

我遇到过这个问题。这是因为 Android 试图将类似的 PendingIntents 合并为 1 个,这会产生一些非常烦人的后果。 IE 较旧的 Intent 在它们略有不同时会被丢弃。我认为它们的折叠方式也存在一个错误,因为它与文档不符。 (我很久以前就遇到过这个,所以事情可能会改变。)

基本上我会确保 PendingIntent.getActivity 中的 requestCode 参数每个 UI 项目都不同。这样他们每个人都有自己的 PendingIntent 并且永远不会发生冲突。

我这里有代码试图解决这个问题,但有点浪费。 http://code.google.com/p/futonic-mylocationwidget/source/browse/src/com/futonredemption/mylocation/Intents.java#35

我承认我的代码中的解决方案不是最好的,但到目前为止它似乎工作得很好。

关于android - 同一 Intent 的 Widget 上的多个 OnClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6891620/

相关文章:

android - 在 UPC300-2.2 Viewsonic G-Tablet 上找不到 NetworkConnectivityListener 类

java - 在 Firebase Firestore 中保存自定义数据类型

symfony1 - symfony 1.4 将图像显示为选择小部件中的选项

android - 广播接收器未被 twilio 解雇

android - 字符串 xml 语言环境没有改变

android - 如何在android中使用getSharedPreferences

javascript - 使用 React 创建可嵌入的小部件

flutter - 如何遍历 map 列表并在 flutter ListView 中显示文本小部件?

android - Intent - 如果 Activity 正在运行,请将其放在前面,否则启动一个新 Activity (来自通知)

java - Android:阻止 locationManager 在 list 中注册的广播接收器内更新