Notification的使用

来源:互联网 发布:啪啪啪软件下载 编辑:程序博客网 时间:2024/04/30 06:22
通过Notification我们可以在状态栏、锁屏界面得到相应的提示信息。在android
5.0以后有了很大改进,当你长按Notification的时候,会显示消息来源。
现在我们来说一下基本的Notification的使用。
首先通过Notification.Builder创建一个Notification的builder,然后给
点击Notification后要执行的操作创建一个PendingIntent,给builder设置
一些属性,最后使用NotificationManager系统服务来帮助我们管理Notification
,并通过调用notify方法来发出Notification。
代码如下:
Notification.Builder builder = new Notification.Builder(this);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("标题");
builder.setContentText("内容");
builder.setSubText("副标题");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
以上是基本的Notification的用法。
接下来我们说一下折叠式Notification的用法。
折叠式Notification是一种自定义视图,常常用于显示长文本。
它有两个视图状态,一个是普通状态下的视图状态,另一个是展开状态下的
视图状态。我们需要使用RemoteViews来帮助我们创建一个自定义的Notification视图。代码如下:
Notification.Builder builder = new Notification.Builder(this);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.tv, "折叠时");
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
Notification notification = builder.build();
notification.contentView = contentView;
notification.bigContentView = expandedView;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
悬挂式Notification,通过在屏幕上方产生Notification且不会打断用户操作。
代码如下:
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setPriority(Notification.PRIORITY_DEFAULT);
builder.setCategory(Notification.CATEGORY_MESSAGE);
builder.setContentTitle("标题");
builder.setContentText("内容");
builder.setAutoCancel(true);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setFullScreenIntent(pendingIntent, true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
在android5.x中将Notification分成了三个等级:
VISIBILITY_PRIVATE--表明只有当没有锁屏的时候会显示。
VISIBILITY_PUBLIC--表明在任何情况下都会显示。
VISIBILITY_SECRET--表明在pin、password等安全锁和没有锁屏的情况下才能够显示。
然后在代码中设置,builder.setVisibility(Notification.VISIBILITY_PUBLIC);
0 0
原创粉丝点击