1

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

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 如果我不使用 R.id.button 我使用 rv.getLayoutId() 那么按钮不响应。我希望每个按钮都应该在 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>
4

1 回答 1

1

将此行更改为:

  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。

于 2016-04-18T06:22:26.640 回答