100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android通知栏使用情况 Android通知栏(Notification)介绍及使用

android通知栏使用情况 Android通知栏(Notification)介绍及使用

时间:2020-02-17 17:45:10

相关推荐

android通知栏使用情况 Android通知栏(Notification)介绍及使用

在使用手机时,咱们经常会碰到各类通知,例如微信,头条,UC等,每天不厌其烦的给你各类推送,固然了咱们今天不讲推送,咱们讲讲通知栏的构建和使用,以及自定义通知栏的布局和使用方法java

构建一个通知栏通常分为这几个步骤:git

1.建立通知栏管理工具

2.构建通知栏构造器

3.给构造器设置参数

4.发送请求github

具体代码以下:web

/**

* 建立通知栏管理工具

*/

NotificationManager notificationManager = (NotificationManager) getSystemService

(NOTIFICATION_SERVICE);

/**

* 实例化通知栏构造器

*/

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

/**

* 设置Builder

*/

//设置标题

mBuilder.setContentTitle("我是标题")

//设置内容

.setContentText("我是内容")

//设置大图标

.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))

//设置小图标

.setSmallIcon(R.mipmap.ic_launcher_round)

//设置通知时间

.setWhen(System.currentTimeMillis())

//首次进入时显示效果

.setTicker("我是测试内容")

//设置通知方式,声音,震动,呼吸灯等效果,这里通知方式为声音

.setDefaults(Notification.DEFAULT_SOUND);

//发送通知请求

notificationManager.notify(10, mBuilder.build());

由于我在代码中注释的比较清楚,这里就不一一赘述了微信

显然直接使用原生的通知栏会在不通顺手机上显示不一样效果,没法造成统一性,也不是特别美观,因此咱们须要自定义通知栏svg

自定义通知栏和使用原生的通知栏区别不大,最主要就是增长了自定义的布局,使用RemoteViews承接,并放入构造器中显示,具体代码以下工具

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);

NotificationManager notificationManager = (NotificationManager) getSystemService

(NOTIFICATION_SERVICE);

mBuilder.setSmallIcon(R.mipmap.timg);

mBuilder.setContent(remoteViews);

if (progress == 1) {

mBuilder.setDefaults(Notification.DEFAULT_SOUND);

}

remoteViews.setImageViewResource(R.id.image, R.mipmap.timg);

remoteViews.setTextViewText(R.id.title, "我是标题");

remoteViews.setTextViewText(R.id.content, "我是内容");

remoteViews.setProgressBar(R.id.pBar, 10, progress, false);

remoteViews.setTextViewText(R.id.proNum, progress + "/10");

notificationManager.notify(10, mBuilder.build());

点击跳转到其余页面:布局

Intent intent = new Intent(this, SecondeActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

mBuilder.setContentIntent(pendingIntent);

到此,具体工做就已经完成的差很少了,你会用了吗,固然还有不少知识点,想学更多的还有能够去看具体源码post

遇到的问题:

(1)

在Android O及以上版本报Developer warning for package “”

Failed to post notification on channel “null”

see log for more details 错误,错误缘由是由于在8.0以上须要增长渠道名称和渠道ID,具体代码以下:测试

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

NotificationChannel channel = new NotificationChannel(id, name, NotificationManager

.IMPORTANCE_DEFAULT);

mBuilder.setChannelId(id);

notificationManager.createNotificationChannel(channel);

mBuilder.setSmallIcon(R.mipmap.timg);

mBuilder.setContent(remoteViews);

if (progress == 1) {

mBuilder.setDefaults(Notification.DEFAULT_SOUND);

}

remoteViews.setImageViewResource(R.id.image, R.mipmap.timg);

remoteViews.setTextViewText(R.id.title, "我是标题");

remoteViews.setTextViewText(R.id.content, "我是内容");

remoteViews.setProgressBar(R.id.pBar, 10, progress, false);

remoteViews.setTextViewText(R.id.proNum, progress + "/10");

notificationManager.notify(10, mBuilder.build());

}

(2)mBuilder.setSmallIcon()是必需要加上的,这个是显示在顶部状态栏中的小图标,若是未加这个图标程序将会闪退,并报如下错误

java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=cm.cui.testnotification/0x7f09001d vibrate=null sound=default defaults=0x1 flags=0x0 color=0x00000000 vis=PRIVATE)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。