gpt4 book ai didi

android - 在 for 循环的自定义通知中动态添加 RemoteViews,设置 pendingintent 时出现问题

转载 作者:行者123 更新时间:2023-11-30 01:24:03 25 4
gpt4 key购买 nike

我正在创建自定义通知并使用此代码动态添加按钮:

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

for (int i = 0; i < 100; i++) {

Intent mybuttonsIntent = new Intent(this, NotifyActivityHandler.class);
RemoteViews rv = new RemoteViews(this.getPackageName(), R.layout.button_layout);
Bundle b = new Bundle();
b.putInt("abcd", i);
mybuttonsIntent.putExtras(b);
rv.setOnClickPendingIntent(R.id.button, PendingIntent.getActivity(this, 0, mybuttonsIntent, PendingIntent.FLAG_UPDATE_CURRENT));
remoteViews.addView(R.id.rlMainNotification, rv);
}
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.setContent(remoteViews);
NotificationManager notificationmanager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationmanager.notify(0, builder.build());

这里是 button_layout.xml:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_weight="1" />

在 NotifyActivityHandler 中,我使用这段代码来获取 Int:

Bundle extras = getIntent().getExtras();
if (extras != null) {
int level = extras.getInt("abcd");
Toast.makeText(this, "testing" + level, Toast.LENGTH_LONG).show();

但问题是每个按钮都传递相同的 Int 即 99如果我使用 rv.getLayoutId() 而不是使用 R.id.button,则按钮不会响应。我希望每个按钮都应该在 for 循环中分别传递分配给它的值。

编辑:应评论者的要求,这里是 custom_notification 的 xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">

<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">

<ImageView
android:id="@+id/autoIcon"
android:tint="@color/themeColor"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="@drawable/ic_brightness_medium_white_48dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>

<LinearLayout
android:id="@+id/rlMainNotification"
android:layout_width="0dp"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:layout_height="match_parent"
android:paddingRight="4dp"
android:paddingLeft="2dp"
android:layout_weight="9"
>

</LinearLayout>

</LinearLayout>

最佳答案

将此行更改为:

  rv.setOnClickPendingIntent(R.id.button, PendingIntent.getActivity(this, 0, mybuttonsIntent, PendingIntent.FLAG_UPDATE_CURRENT));

  rv.setOnClickPendingIntent(R.id.button, PendingIntent.getActivity(this, i, mybuttonsIntent, PendingIntent.FLAG_UPDATE_CURRENT));

基本上把 0 改成 i。

关于android - 在 for 循环的自定义通知中动态添加 RemoteViews,设置 pendingintent 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com