自定义Android通知布局并单击元素

时间:2015-07-31 03:07:19

标签: android layout notifications

我想在通知面板中显示来自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等。我没有找到任何东西来处理每个元素的点击。

1 个答案:

答案 0 :(得分:1)

使用remoteView.setOnClickPendingIntent(viewId, pendingIntent)为RemoteViews对象中的视图提供单击操作。这将允许您执行诸如启动服务或发送广播(由BroadcastReceiver接收)之类的操作。