setOnClickPendingIntent в уведомлении не работает на Android N? Деталь: нужно дважды щелкнуть? Почему?
Код:
intent = new Intent(ACTION_STOP);
intent.setClass(context, SoundRecorderService.class);
pIntent = PendingIntent.getService(context, 0, intent, 0);
mNotificationView.setOnClickPendingIntent(R.id.btn_stop, pIntent);
Notification.Builder builder=new Notification.Builder(context);
Notification notification = builder.build();
notification.contentView = mNotificationView;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.icon = R.drawable.notification_ic_small;
notification.contentIntent = pIntent;
startForeground(START_NOTIFICATION_ID, notification);
Эти коды хорошо в android M
, Но это работает ненормально в android N
, Когда мы впервые нажимаем btn_stop
кнопка, телефонные системы не отправляют это PendingIntent
так что не запускается SoundRecorderService.class
, Тогда мы нажимаем на это снова, это работает.
2 ответа
Ошибка хорошо видна на эмуляторе, где вы можете сделать любое количество нажатий на одну и ту же точку. Суть ошибки заключается в том, что клики периодически теряются в области пользовательских уведомлений. Этот код хорошо работает в Android L, M и O.
Пример простого макета с не работающими кликами, если высота для ImageButton установлена на "match_parent". Решение, которое работало для меня, состояло в том, чтобы установить определенное значение для высоты ("64dp").
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:layout_height="64dp">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageButton
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/ic_notify_1"/>
</FrameLayout>
</LinearLayout>
Сожалею! Мое уведомление об удаленном просмотре исправляет одну точку, код ошибки:
<ImageView
android:id="@+id/btn_stop"
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_gravity="center_vertical"
android:background="@drawable/notification_background"
android:focusable="true"
android:focusableInTouchMode="true"
android:src="@drawable/notification_stop"
android: focusableInTouchMode = "true" исправить для android:clickable="true" правильный код:
<ImageView
android:id="@+id/btn_stop"
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_gravity="center_vertical"
android:focusable="true"
android:clickable="true"
android:src="@drawable/notification_stop" />