微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android锁屏通知自定义视图带波纹和双击

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

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

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

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

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

PS:我正在针对API 19,我想为通知使用自定义视图布局以及setonClickPendingIntent,因为只有此侦听器允许打开任何安全模式的设备的活动.

解决方法

方法publishNotificationWithCustomView中删除setonClickPendingIntent,并将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);
}

然后从lock_screen_notification.xml和lock_screen_notification_android_n.xml中删除android:clickable =“true”:

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

    ....

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐