2011-06-02 66 views
2

我想在每个通知中添加一个按钮...并且用户可以单击该按钮以删除单个通知,我看到很多人说只是参考“创建自定义展开视图”并使用RemoteViews,但是可以修改官方代码并让按钮工作吗? 我曾在“status_bar_latest_event_context.xml”按钮添加使用的ImageButton如何在Android通知中添加按钮

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:paddingTop="3dp" 
    > 
    <!--com.android.server.status.AnimatedImageView android:id="@+id/icon" --> 
    <ImageView android:id="@+id/icon" 
     android:layout_width="25dp" 
     android:layout_height="25dp" 
     android:scaleType="fitCenter" 
     android:src="@drawable/arrow_down_float"/> 
    <TextView android:id="@+id/title" 
     android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:singleLine="true" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" 
     android:paddingLeft="4dp" 
     /> 
    <ImageButton android:id="@+id/imgbtn_del" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/btn_close"/> 

</LinearLayout> 

,它会显示在每个通知图像按钮,但我不知道如何让按钮工作。

在StatusBarService.java,我们可以发现

// bind the click event to the content area 
    ViewGroup content = (ViewGroup)row.findViewById(R.id.content); 
    content.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); 
    content.setOnFocusChangeListener(mFocusChangeListener); 
    PendingIntent contentIntent = n.contentIntent; 
    if (contentIntent != null) { 
     content.setOnClickListener(new Launcher(contentIntent, notification.pkg, 
        notification.tag, notification.id)); 
    } 

它的单击事件绑定到内容区域。所以我不能点击按钮。 我不知道如何修改源代码以及如何设置OnClick函数..

请帮忙... 非常感谢!

回答

0

我希望这可以帮助你

//Exemple of notification with Button 
private static void scheduleNotificationWithButton(Context context, String message) { 

    int notifReCode = 1; 

    //What happen when you will click on button 
    Intent intent = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT); 

    //Button 
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.my_image, "Delete", pendingIntent).build(); 

    //Notification 
    Notification notification = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentText("Back to Application ?") 
      .setContentTitle("Amazing news") 
      .addAction(action) //add buton 
      .build(); 

    //Send notification 
    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, notification); 
}