当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发之Notification通知用法详解

Android开发之Notification通知用法详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

12人吃西瓜中毒,黛芙薇尔两周期多少钱,嘻哈四重奏之花落台北

本文实例讲述了android开发之notification通知用法。分享给大家供大家参考,具体如下:

根据activity的生命周期,在activity不显示时,会执行onstop函数(比如按下home键),所以你在onstop函数(按退出键除外)里面把notification放在通知栏里,再此显示时,把notification从通知栏里去掉。或者,只要程序在运行就一直显示通知栏图标。

下面对notification类中的一些常量,字段,方法简单介绍一下:

常量:

default_all 使用所有默认值,比如声音,震动,闪屏等等
default_lights 使用默认闪光提示
default_sounds 使用默认提示声音
default_vibrate 使用默认手机震动

【说明】:加入手机震动,一定要在manifest.xml中加入权限:

<uses-permission android:name="android.permission.vibrate" />

以上的效果常量可以叠加,即通过

notification.defaults =default_sound|default_vibrate; 

notification.defaults |= default_sound (最好在真机上测试,震动效果模拟器上没有)

设置flag位

flag_auto_cancel 该通知能被状态栏的清除按钮给清除掉
flag_no_clear 该通知能被状态栏的清除按钮给清除掉
flag_ongoing_event 通知放置在正在运行
flag_insistent 是否一直进行,比如音乐一直播放,知道用户响应

常用字段:

contentintent 设置pendingintent对象,点击时发送该intent
defaults 添加默认效果
flags 设置flag位,例如flag_no_clear等
icon 设置图标
sound 设置声音
tickertext 显示在状态栏中的文字
when 发送此通知的时间戳

notificationmanager常用方法介绍:

public void cancelall() 移除所有通知(只是针对当前context下的notification)
public void cancel(int id) 移除标记为id的通知 (只是针对当前context下的所有notification)
public void notify(string tag ,int id, notification notification) 将通知加入状态栏,标签为tag,标记为id
public void notify(int id, notification notification) 将通知加入状态栏,标记为id

package com.ljq.activity;
import android.app.activity;
import android.app.notification;
import android.app.notificationmanager;
import android.app.pendingintent;
import android.content.intent;
import android.graphics.color;
import android.os.bundle;
public class mainactivity extends activity {
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
clearnotification();
}
@override
protected void onstop() {
shownotification();
super.onstop();
}
@override
protected void onstart() {
clearnotification();
super.onstart();
}
/**
* 在状态栏显示通知
*/
private void shownotification(){
// 创建一个notificationmanager的引用 
notificationmanager notificationmanager = (notificationmanager) 
this.getsystemservice(android.content.context.notification_service); 
// 定义notification的各种属性 
notification notification =new notification(r.drawable.icon, 
"督导系统", system.currenttimemillis()); 
//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一起使用 
notification.flags |= notification.flag_show_lights; 
//default_all 使用所有默认值,比如声音,震动,闪屏等等
//default_lights 使用默认闪光提示
//default_sounds 使用默认提示声音
//default_vibrate 使用默认手机震动,需加上<uses-permission android:name="android.permission.vibrate" />权限
notification.defaults = notification.default_lights; 
//叠加效果常量
//notification.defaults=notification.default_lights|notification.default_sound;
notification.ledargb = color.blue; 
notification.ledonms =5000; //闪光时间,毫秒
// 设置通知的事件消息 
charsequence contenttitle ="督导系统标题"; // 通知栏标题 
charsequence contenttext ="督导系统内容"; // 通知栏内容 
intent notificationintent =new intent(mainactivity.this, mainactivity.class); // 点击该通知后要跳转的activity 
pendingintent contentitent = pendingintent.getactivity(this, 0, notificationintent, 0); 
notification.setlatesteventinfo(this, contenttitle, contenttext, contentitent); 
// 把notification传递给notificationmanager 
notificationmanager.notify(0, notification); 
}
//删除通知 
private void clearnotification(){
// 启动后删除之前我们定义的通知 
notificationmanager notificationmanager = (notificationmanager) this 
.getsystemservice(notification_service); 
notificationmanager.cancel(0); 
}
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android基本组件用法总结》、《android视图view技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android编程之activity操作技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网