同一意图的小部件上的多个 OnClick

发布于 2024-11-27 03:07:28 字数 2514 浏览 0 评论 0原文

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

我想在每个 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) {

                    }
        }

}

My Widget starts a Service and the Service Update a List of 3 LinearLayouts.

I want to set on each LinearLayout a SetOnClickPeningIntent with different Extra

But when i start the widget and want to click on the LinearLayouts only the last one is onclickable =/ I dont know what's wrong. Hope you can help me.

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) {

                    }
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-12-04 03:07:28

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

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

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

我会承认我的解决方案代码不是最好的,但到目前为止似乎运行得很好。

I've had this problem. It's because Android tries to collapse similar PendingIntents into 1 one and this has some really annoying consequences. IE the older intents are dropped when they are subtly different. I think there's also a bug in how they do collapsing as well since it doesn't match the documentation. (I ran into this a long time ago so things could have changed.)

Basically I make sure that the requestCode parameter in PendingIntent.getActivity is different for each UI item. That way they each get their own PendingIntent and never collide.

I have code here that tries to get around the problem but it's a bit wasteful.
http://code.google.com/p/futonic-mylocationwidget/source/browse/src/com/futonredemption/mylocation/Intents.java#35

I will admit the solution in my code isn't the best but it seems to work pretty well so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文