как добавить кнопку в уведомления Android - PullRequest
2 голосов
/ 02 июня 2011

Я пытаюсь добавить кнопку в каждое уведомление ... и пользователь может нажать кнопку, чтобы удалить отдельное уведомление, я видел, что многие люди говорят, что просто ссылаются на «Создание пользовательского расширенного представления» и использование RemoteViews, но возможно ли этоизменить официальный код и позволить кнопке работать?Я добавил кнопку в "status_bar_latest_event_context.xml", используя imagebutton

<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 ..

, пожалуйста, помогите ... Спасибо! Огромное!

1 Ответ

0 голосов
/ 16 сентября 2016

Я надеюсь, что это может помочь вам

//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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...