安卓Notification的几个常见难点

来源:互联网 发布:java web get post 编辑:程序博客网 时间:2024/05/19 18:12


1. 如何实现自定义的通知布局?

重点: Remoteviews类


2. 为何无法显示通知(关于安卓的一个默认设定)

重点:setsmallIcon方法

设置通知栏小图标,这个方法是必须的,否则无法成功创建合法的notification实例,切记!


3.如何显示完整的通知消息(较长的通知消息)

重点:bigContentviews属性 或者 setStyle方法

具体代码和分析如下:

<span style="font-size:24px;">... PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);        NotificationManager mNotifM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_layout);//Remoteviews类是Notification视图使用的view类,构造方法的第一个参数是packagename,是必须的        remoteViews.setTextViewText(R.id.notification_message, title);        remoteViews.setTextViewText(R.id.notification_time, getNotifytime());//自定义的view中的文本控件,getNotifytime 是我自定义的获取当前时间的字符串的方法,无关紧要        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);        mBuilder.setSmallIcon(R.drawable.app_ent_120x120)//设置通知栏小图标,这个方法是必须的,否则无法成功创建合法的notification实例,切记!                .setAutoCancel(true)//点击通知后自动取消显示当前的通知                .setContentIntent(contentIntent);        if (!hasSound) {            mBuilder.setDefaults(Notification.DEFAULT_SOUND);        }         Notification customNotification = mBuilder.build();        customNotification.contentView = remoteViews;//将自定义的View作为显示通知的普通状态的view(即文本一般只显示1行或者2行)        if (Build.VERSION.SDK_INT > 15)            customNotification.bigContentView = remoteViews;//将自定义的View作为显示通知的大文本的view(能显示5行左右的消息), 还有一种方法是在bulider执行方法.setStyle(new NotificationCompat.BigTextStyle().bigText(title))设置将通知文本作为大文本显示(显示全部的信息),不过显示的风格是系统默认的        mNotifM.notify(NOTIFICATION_ID, customNotification);...</span>



0 0
原创粉丝点击