[控件]NotificationManager提示通知(三)自定义通知

要创建一个自定义的Notification,可以使用RemoteViews。要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给contentIntent字段
int icon = R.drawable.icon; // 通知图标
CharSequence tickerText = "Hello"; // 状态栏显示的通知文本提示
long when = System.currentTimeMillis(); // 通知产生的时间,会在通知信息里显示
// 用上面的属性初始化Nofification
Notification notification = new Notification(icon, tickerText,
        when);
RemoteViews contentView = new RemoteViews(getPackageName(),
        R.layout.view);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text, "Hello,this is JC");
notification.contentView = contentView;
Intent notificationIntent = new Intent(mainActivity.this,
        otherActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
        mainActivity.this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
// 把Notification传递给NotificationManager
notificationManager.notify(0, notification);

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(扩展)