2011-06-02 25 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); 
}