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

Android 通知栏Notification学习日志

2012-12-12 13:55 316 查看
Notification 一般情况下在当服务接收到通知后中启动。

Notification 启动的2个条件:

①:创建NotificationManager(通知管理器)对象,代码:

NotificationManager notificationManager = (NotificationManager)
this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

②:创建Notification对象,并定义Notification的各种属性,代码

//启动通知的方法,参数moodId图标,  textId走马灯文字,  showTicker是否显示走马灯提示
private void setMood(int moodId, int textId, boolean showTicker) {

//是否显示走马灯提示
String tickerText = showTicker ? getString(textId) : null;

// 创建Notification对象,并定义Notification的各种属性
Notification notification = new Notification();
notification.icon = moodId;//设置图标
notification.tickerText = tickerText;//设置走马灯文字
notification.defaults |= Notification.DEFAULT_SOUND;//设置是否开启通知声音
notification.flags |= Notification.FLAG_INSISTENT;//设置声音循环

notification.defaults |= Notification.DEFAULT_VIBRATE;//设置是否震动
long[] vibrate = {0,100,200,300};//用一个long形的数组来表示震动,第一个值表示开始震动的时间,第二个值表示第一次震动的时长,第三个值下一次震动的时间以此类推,这个数组可以按照你需要进行设置,但是我们不能设置重复震动。
notification.vibrate = vibrate;

//设置闪屏
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

//FLAG_AUTO_CANCEL   该通知能被状态栏的清除按钮给清除掉,(点了通知详情后通知栏的图标消失)
//FLAG_NO_CLEAR      该通知不能被状态栏的清除按钮给清除掉
//FLAG_ONGOING_EVENT 通知放置在正在运行
//FLAG_INSISTENT     是否一直进行,比如音乐一直播放,知道用户响应
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用

//定义 RemoteViews 并设置设置其显示内容RemoteViews (String packageName, int layoutId)
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
//设置消息详细的样式
contentView.setImageViewResource(R.id.image, moodId);
contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view. My mood is:" +tickerText);
notification.contentView = contentView; //显示详细内容绑定
notification.contentIntent = makeMoodIntent(moodId);//PendingIntent绑定

//由通知管理器发送 该通知
mNotificationManager.notify(R.layout.activity_main, notification);
}

//返回一个PendingIntent的方法
private PendingIntent makeMoodIntent(int moodId) {
Intent intent = new Intent(this, MainActivity.class).putExtra("moodimg", moodId);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
return contentIntent;
}


PendingIntent 是对Intent的包装,主要用于在用户点击Notification后的动作,此处我们为什么要使用PendingIntent 呢?大家都知道使用Intent可以启动Activity , Service 或发送Broadcast,当用户点击一个Notification后,由系统发出一条Intent,如果我们不告知此Intent用于启动什么的话,系统是不知道此Intent是用来启动Activity , Service 还是发送Broadcast。

在上述代码中我们先创建一个Intent对象,让后再用PendingIntent对其封装。

注意在创建Intent对象的时候一定要设置其Flags,其中Intent.FLAG_ACTIVITY_NEW_TASK 标记表示启动一个新的Activity,系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity,而Intent.FLAG_ACTIVITY_CLEAR_TOP表示如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: