安卓锁屏通知自定义视图,带有波纹和双击

Lau*_*ent 10 android views lockscreen android-notifications ripple

我正在开发一款Android应用.最后一次使用带有锁定屏幕上显示的自定义视图的通知.不幸的是,当我像其他通知一样点击它时,我无法获得波纹和高程效果.此外,单个触摸触发我已配置的意图,而其他通知需要双击.

我在Github上放了一个最小的项目示例:

https://github.com/lpellegr/android-notification-custom-example

应用程序示例提供了两个用于发布通知的按钮:一个使用自定义视图并受到上述问题的影响,另一个通知使用具有预期行为的默认系统视图.

在此输入图像描述

任何关于如何获得波纹和高程效果以及双击行为(通过保持自定义视图)的想法都是受欢迎的.

PS:我的目标是API 19+,我想使用自定义视图布局和setOnClickPendingIntent,因为只有这个监听器允许打开活动,无论设备的安全模式是什么.

Mat*_*ini 3

setOnClickPendingIntent从方法中删除publishNotificationWithCustomView并添加setContentIntent到通知生成器:

private void publishNotificationWithCustomView() {
    String title = "Notification Custom View";
    String content = "No ripple effect, no elevation, single tap trigger";
    Context context = getApplicationContext();

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setWhen(System.currentTimeMillis())
                    .setDefaults(DEFAULT_ALL)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setOnlyAlertOnce(true)
                    .setAutoCancel(false)
                    .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                    .setContentTitle(title)
                    .setContentText(content)
                    .setOngoing(true)
                    .setCategory(NotificationCompat.CATEGORY_ALARM)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setContentIntent(createLockscreenNotificationPendingIntent(context));

    int notificationLayoutResId = R.layout.lock_screen_notification;

    // using folder layout-vX is having issue with LG devices
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        notificationLayoutResId = R.layout.lock_screen_notification_android_n;
    }

    RemoteViews remoteView = new RemoteViews(
            context.getPackageName(), notificationLayoutResId);
    remoteView.setTextViewText(R.id.title, title);
    remoteView.setTextViewText(R.id.text, content);

    builder.setCustomContentView(remoteView);

    Notification notification = builder.build();
    publishNotification(context, notification, 7);
}
Run Code Online (Sandbox Code Playgroud)

然后android:clickable="true"lock_screen_notification.xml和 中删除lock_screen_notification_android_n.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="64dp">

    ....
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的建议。不幸的是,如果我使用 _setContentIntent_ 而不是 _setOnClickPendingIntent_,当设备使用架构、引脚等进行保护时,意图需要解锁锁定屏幕才能查看活动。设置 _setOnClickPendingIntent_ 后,无论安全模式是什么,活动都会在不解锁的情况下打开。因此,您的建议对我来说无效。 (2认同)