AndroidStudioNotification(状态栏通知)详解
1.设计⽂档部分解读
1)Notification的基本布局
上⾯的组成元素依次是:
Icon/Photo:⼤图标
Title/Name:标题
Message:内容信息
Timestamp:通知时间,默认是系统发出通知的时间,也可以通过setWhen()来设置
Secondary Icon:⼩图标
内容⽂字,在⼩图标的左⼿边的⼀个⽂字
2)扩展布局
在 Jelly Bean 中你可以为通知提供更多事件的细节。你可以通过扩展布局显⽰消息的前⼏⾏或者图⽚的预览。这样⽤户可以看多更多的内容 - 有时甚⾄可以看到整个消息。⽤户可以通过 pinch-zoom 或者双⼿指滑动来打开扩展布局。Android 为单条消息提供了两种扩展布局 (⽂字和图像) 供你开发应⽤时使⽤。
关于其他⼀些设计的东西,就不⼀⼀提及了,有兴趣的⾃⾏查看上⾯提供的API⽂档,知道下这个Notification在4.x以上的版本可以多种多样就好!我们更多的时候关注的是如何写代码使⽤这个东西,下⾯我们就来学习下Notification的⽤法!
2.Notification的基本使⽤流程
状态通知栏主要涉及到2个类:Notification 和NotificationManager
Notification:通知信息类,它⾥⾯对应了通知栏的各个属性
NotificationManager:是状态栏通知的管理类,负责发通知、清除通知等操作。
使⽤的基本流程:
Step 1. 获得NotificationManager对象: NotificationManager mNManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Step 2. 创建⼀个通知栏的Builder构造类: Notification.Builder mBuilder = new Notification.Builder(this);
Step 3. 对Builder进⾏相关的设置,⽐如标题,内容,图标,动作等!
Step 4.调⽤Builder的build()⽅法为notification赋值
Step 5.调⽤NotificationManager的notify()⽅法发送通知!
PS:另外我们还可以调⽤NotificationManager的cancel()⽅法取消通知
3.设置相关的⼀些⽅法:
Notification.Builder mBuilder = new Notification.Builder(this);
后再调⽤下述的相关的⽅法进⾏设置:(官⽅API⽂档:) 常⽤的⽅法如下:
setContentTitle(CharSequence):设置标题
setContentText(CharSequence):设置内容
setSubText(CharSequence):设置内容下⾯⼀⼩⾏的⽂字
setTicker(CharSequence):设置收到通知时在顶部显⽰的⽂字信息
setWhen(long):设置通知时间,⼀般设置的是收到通知时的System.currentTimeMillis()
setSmallIcon(int):设置右下⾓的⼩图标,在接收到通知的时候顶部也会显⽰这个⼩图标
setLargeIcon(Bitmap):设置左边的⼤图标
setAutoCancel(boolean):⽤户点击Notification点击⾯板后是否让通知取消(默认不取消)
setDefaults(int):向通知添加声⾳、闪灯和振动效果的最简单、使⽤默认(defaults)属性,可以组合多个属性,
Notification.DEFAULT_VIBRATE(添加默认震动提醒);
Notification.DEFAULT_SOUND(添加默认声⾳提醒);
Notification.DEFAULT_LIGHTS(添加默认三⾊灯提醒)
Notification.DEFAULT_ALL(添加默认以上3种全部提醒)
setVibrate(long[]):设置振动⽅式,⽐如:
setVibrate(new long[] {0,300,500,700});延迟0ms,然后振动300ms,在延迟500ms,接着再振动700ms,关于Vibrate⽤法后⾯会讲解!
setLights(int argb, int onMs, int offMs):设置三⾊灯,参数依次是:灯光颜⾊,亮持续时间,暗的时间,不是所有颜⾊都可
以,这跟设备有关,有些⼿机还不带三⾊灯;另外,还需要为Notification设置flags为Notification.FLAG_SHOW_LIGHTS 才⽀持三⾊灯提醒!
setSound(Uri):设置接收到通知时的铃声,可以⽤系统的,也可以⾃⼰设置,例⼦如下:
.setDefaults(Notification.DEFAULT_SOUND) //获取默认铃声
.setSound(Uri.parse("file:///sdcard/xx/xx.mp3")) //获取⾃定义铃声
.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5")) //获取Android多媒体库内的铃声
setOngoing(boolean):设置为ture,表⽰它为⼀个正在进⾏的通知。他们通常是⽤来表⽰⼀个后台任务,⽤户积极参与(如播放⾳乐)或以某种⽅式正在等待,因此占⽤设备(如⼀个⽂件下载, 同步操作,主动⽹络连接)
setProgress(int,int,boolean):设置带进度条的通知参数依次为:进度条最⼤数值,当前进度,进度是否不确定如果为确定的进度条:调⽤setProgress(max, progress, false)来设置通知,在更新进度的时候在此发起通知更新progress,并且在下载完成后要移除进度条,通过调⽤setProgress(0, 0, false)既可。如果为不确定(持续活动)的进度条,这是在处理进度⽆法准确获知时显⽰活动正在持续,所以调⽤setProgress(0, 0, true) ,操作结束时,调⽤setProgress(0, 0, false)并更新通知以移除指⽰条
setContentIntent(PendingIntent):PendingIntent和Intent略有不同,它可以设置执⾏次数,主要⽤于远程服务通信、闹
铃、通知、启动器、短信中,在⼀般情况下⽤的⽐较少。⽐如这⾥通过 Pending启动Activity:getActivity(Context, int,
Intent, int),当然还可以启动Service或者Broadcast PendingIntent的位标识符(第四个参数):
FLAG_ONE_SHOT 表⽰返回的PendingIntent仅能执⾏⼀次,执⾏完后⾃动取消
FLAG_NO_CREATE 表⽰如果描述的PendingIntent不存在,并不创建相应的PendingIntent,⽽是返回NULL
FLAG_CANCEL_CURRENT 表⽰相应的PendingIntent已经存在,则取消前者,然后创建新的PendingIntent,这个有利于数据保持为最新的,可以⽤于即时通信的通信场景
FLAG_UPDATE_CURRENT 表⽰更新的PendingIntent
使⽤⽰例:
//点击后跳转Activity
Intent intent = new Intent(context,XXX.class);
PendingIntent pendingIntent = Activity(context, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent)
setPriority(int):设置优先级:
优先级⽤户
android最新版
MAX重要⽽紧急的通知,通知⽤户这个事件是时间上紧迫的或者需要⽴即处理的。
HIGH⾼优先级⽤于重要的通信内容,例如短消息或者聊天,这些都是对⽤户来说⽐较有兴趣的。
DEFAULT默认优先级⽤于没有特殊优先级分类的通知。
LOW低优先级可以通知⽤户但⼜不是很紧急的事件。
MIN⽤于后台消息 (例如天⽓或者位置信息)。最低优先级通知将只在状态栏显⽰图标,只有⽤户下拉通知抽屉才能看到内容。
对应属性:Notification.
4.代码⽰例:最常见的Notification:
下⾯我们来写⼀个最简单的例⼦来体验下Notification的⽤法:
关键代码:
这⾥直接贴MainActivity.java的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Context mContext;
private NotificationManager mNManager;
private Notification notify1;
Bitmap LargeBitmap = null;
private static final int NOTIFYID_1 = 1;
private Button btn_show_normal;
private Button btn_close_normal;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
//创建⼤图标的Bitmap
LargeBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.iv_lc_icon);
mNManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
bindView();
}
private void bindView() {
btn_show_normal = (Button) findViewById(R.id.btn_show_normal);
btn_close_normal = (Button) findViewById(R.id.btn_close_normal);
btn_show_normal.setOnClickListener(this);
btn_close_normal.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_show_normal:
//定义⼀个PendingIntent点击Notification后启动⼀个Activity
Intent it = new Intent(mContext, OtherActivity.class);
PendingIntent pit = Activity(mContext, 0, it, 0);
//设置图⽚,通知标题,发送时间,提⽰⽅式等属性
Notification.Builder mBuilder = new Notification.Builder(this);
mBuilder.setContentTitle("叶良⾠")                        //标题
.
setContentText("我有⼀百种⽅法让你呆不下去~")      //内容
.setSubText("——记住我叫叶良⾠")                    //内容下⾯的⼀⼩段⽂字
.setTicker("收到叶良⾠发送过来的信息~")            //收到信息后状态栏显⽰的⽂字信息
.setWhen(System.currentTimeMillis())          //设置通知时间
.setSmallIcon(R.mipmap.ic_lol_icon)            //设置⼩图标
.setLargeIcon(LargeBitmap)                    //设置⼤图标
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)    //设置默认的三⾊灯与振动器                        .setSound(Uri.parse("source://" + getPackageName() + "/" + R.raw.biaobiao))  //设置⾃定义的提⽰⾳                        .setAutoCancel(true)                          //设置点击后取消Notification
.setContentIntent(pit);                        //设置PendingIntent
notify1 = mBuilder.build();
break;
case R.id.btn_close_normal:
//除了可以根据ID来取消Notification外,还可以调⽤cancelAll();关闭该应⽤产⽣的所有通知
mNManager.cancel(NOTIFYID_1);                          //取消Notification
break;
}
}
}