Android锁定屏幕通知自定义视图带纹波和双击
问题描述:
我正在开发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">
....
谢谢你的建议。不幸的是,如果我使用_setContentIntent_而不是_setOnClickPendingIntent_,那么当设备使用模式,引脚等进行保护时,意图要求解锁锁定屏幕以查看活动。当设置_setOnClickPendingIntent_时,无论安全模式是什么,该活动都将打开而不解锁。出于这个原因,你的建议对我无效。 – Laurent
@Laurent你有没有找到解决方案? – cristianomad