2016-04-18 37 views
1

我创建一个自定义通知,并使用此代码动态添加按钮Dyanamically中添加自定义通知RemoteViews在for循环,问题:在设定的PendingIntent

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,即如果不使用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> 
+0

rlMainNotification在哪里定义?我试图完成类似的事情,但不成功。你介意张贴布局XML为custom_notification以及 –

+0

这是一个线性布局在custom_notification.xml – Usman

+1

我也已经添加了XML请求。 – Usman

回答

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到我。

+0

感谢兄弟工作:) – Usman