您的位置:首页 > 移动开发 > Android开发

Android Notification 知识点自我小结

2015-03-17 19:49 183 查看
先来点Android官方关于通知的文档(有译文哦~)

官方:http://developer.android.com/design/patterns/notifications.html

译文:http://adchs.github.io/patterns/notifications.html

使用教程 :http://developer.android.com/training/notify-user/index.html

开发文档 :http://developer.android.com/reference/android/app/Notification.html

作为通知的体现者与管理者,Notification跟Notificaition的关系无疑是亲密的:

Notification为通知信息类,它里面对应了通知栏的各个属性

NotificationManager : 是状态栏通知的管理类,负责发通知、清除通知等操作**

NotificationManager mNotificationManager = (NotificationManager)    getSystemService(NOTIFICATION_SERVICE);  //获取一个系统的服务得到NotificationManager的实例
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("测试标题")   //设置通知栏标题
.setContentText("测试内容") //设置通知栏显示内容</span>
.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图
.setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的
.setWhen(System.currentTimeMillis()) //通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
.setOngoing(false)  //ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
//Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
.setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON
mNotificationManager.notify(notifyId, mBuilder.build());


在这里要特别说下这个方法notification.setLatestEventInfo(this, title, content, contentIntent),其实在新的版本上,已经不推荐使用了!

notification.flags = Notification.FLAG_AUTO_CANCEL 这个是可以设置用户点击完通知条后自动消失

具备的flag还有以下几种:

Notification.FLAG_SHOW_LIGHTS //三色灯提醒,在使用三色灯提醒时候必须加该标志符

Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中)

Notification.FLAG_INSISTENT //让声音、振动无限循环,直到用户响应 (取消或者打开)

Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次

Notification.FLAG_AUTO_CANCEL //用户单击通知后自动消失

Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)

Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务

通知设置振动:

//设置显示通知时的默认的发声、震动、Light效果

mNotification.defaults = Notification.DEFAULT_VIBRATE

或者 mBuilder.setVibrate(new long[] {0,100,200,300});

mBuilder.build().vibrate = new long[] {0,300,500,700};

具体参数意义:0ms后振动,振动100ms后,停止200ms,再振动300ms

通知设置声音:

mBuilder.setSound(Uri sound)

//获取默认铃声

.setDefaults(Notification.DEFAULT_SOUND)

/获取自定义铃声

.setSound(Uri.parse(“file:///sdcard/xx/xx.mp3”))

//获取Android多媒体库内的铃声

.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, “5”))

自定义通知栏:

自定义通知栏需要自己去设计构图,然后通过RemoteView类实现视图加载。

/**
* 带按钮的通知栏
*/
public void showButtonNotify(){
NotificationCompat.Builder mBuilder = new Builder(this);
RemoteViews mRemoteViews = new RemoteViews(getPackageName(),                R.layout.view_custom_button);
mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.sing_icon);
//API3.0 以上的时候显示按钮,否则消失
mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "周杰伦");
mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "七里香");
//如果版本号低于(3。0),那么不显示按钮
if(BaseTools.getSystemVersion() <= 9){
mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE);
}else{
mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE);
//
if(isPlay){
mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause);
}else{
mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play);
}
}

//点击的事件处理
Intent buttonIntent = new Intent(ACTION_BUTTON);
/* 上一首按钮 */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);
//这里加了广播,所及INTENT的必须用getBroadcast方法
PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev);
/* 播放/暂停  按钮 */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
PendingIntent intent_paly = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_paly);
/* 下一首 按钮  */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);
PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next);

mBuilder.setContent(mRemoteViews)
.setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))
.setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
.setTicker("正在播放")
.setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级
.setOngoing(true)
.setSmallIcon(R.drawable.sing_icon);
Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(200, notify);
}


可参考源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: