android系统通知,Android通知系统之Notification

##Notification

定义:

一种可以显示即时信息的控件,该控件显示在标题栏中,拉开后会看到通知的完整样式

样式:

1. 普通通知

使用普通通知必须的选项

三剑客

设置标题:setContentTitle()

设置图标:setSmallIcon()

添加描述:setContentText()

可选项

setDefaults(系统默认闹铃|系统默认震动)

setTicker(设置标题栏显示的内容。一般为contentText)

创建通知的步骤

定义NotificationComfat.Builder对象

设置必须选项和可选项

发布通知

NotifacationManager.notify(id,Notification)

点击事件

使用PendingIntent,具体方法如下

getActivity(Context context,int requestCode,int flags)

context:获取上下文对象

requestCode:获取另一个Activity的返回码

intent:PendingIntent需要触发的事件,比如跳转Activity

flags:通知标志位

使用setContentIntent(PendingIntent)将封装好的PendingIntent对

象放入该对象中

2. 大视图通知

先创建一个简单通知

使用NotificationCompat.InboxStyle对象设置大视图通知的样式,

代码如下

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

.setSmallIcon(R.drawable.notification_icon)

.setContentTitle("Event tracker")

.setContentText("Events received")

NotificationCompat.InboxStyle inboxStyle =

new NotificationCompat.InboxStyle();

String[] events = new String[6];

// Sets a title for the Inbox style big view

inboxStyle.SetBigContentTitle("Event tracker details:");

...

// Moves events into the big view

for (int i=0; i < events.length; i++) {

inboxStyle.addLine(events[i]);

}

// Moves the big view style object into the notification object.

mBuilder.setStyle(inBoxStyle);

3. 进度条通知

创建一个普通通知

开启一个线程,在改线程中使用setProgress(进度条的最大值,当前进度值,false),在notify方法即时通知进度条,代码如下

mBuilder.setProgress(100,incr,false);

mNotifyManager.notify(0,mBuilder.build());

4. 自定义通知

创建一个普通通知

创建一个自定义布局

创建一个RemoteViews对象,将xml布局加载到对象中,代码如下

RemoteView content =new RemoteViews(getPackageName(),R.layout.item);

使用setContent(content)方法,将RemoteViews对象加载到普通通知对象中

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值