自定义 Android 通知布局和元素点击
Customize Android Notification layout and click of elements
为此,我想在通知面板中显示来自 Url 的图像 我正在尝试使用以下代码自定义 UI 但它不起作用
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(1, notification);
以及我如何处理每个元素(如图像 TextView 等)的点击。我还没有找到任何东西来处理每个元素的点击。
使用 remoteView.setOnClickPendingIntent(viewId, pendingIntent)
为 RemoteViews 对象中的视图提供点击操作。这将允许您执行诸如启动服务或发送广播(由 BroadcastReceiver 接收)之类的事情。
为此,我想在通知面板中显示来自 Url 的图像 我正在尝试使用以下代码自定义 UI 但它不起作用
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(1, notification);
以及我如何处理每个元素(如图像 TextView 等)的点击。我还没有找到任何东西来处理每个元素的点击。
使用 remoteView.setOnClickPendingIntent(viewId, pendingIntent)
为 RemoteViews 对象中的视图提供点击操作。这将允许您执行诸如启动服务或发送广播(由 BroadcastReceiver 接收)之类的事情。