今天总结了一下Notification的使用,与大家分享一下。

MainActivity.java:

本文参考:http://www.jb51.net/article/36567.htmhttp://www.cnblogs.com/linjiqin/archive/2011/12/14/2288074.html

public class MainActivity extends Activity {

    private Button btn;
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// showNotification();
// showNotification2();
// showNotification3();
// showNotification4();
// showNotification5();
showNotification6();
this.btn = (Button) this.findViewById(R.id.button1);
this.btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
clearNotification();
}
});
}
/**
* 自定义Notification布局
*/
private void showNotification6() {
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.custom_notification);
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, 0);
//点击时给SecondActivity发送一条广播
remoteViews.setOnClickPendingIntent(R.id.paly_pause_music,
pendingIntent);
Intent intent1 = new Intent(this,SecondActivity.class);
remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, PendingIntent.getActivity(this, 0, intent1, 0));
NotificationCompat.Builder builder = new Builder(this);
builder.setContent(remoteViews).setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)).setOngoing(true)
.setTicker("music is playing");
manager.notify(0, builder.build());
}
/**
* 大布局Picture类型notification
* 这个方法貌似还有一点问题
*/
private void showNotification5() {
NotificationCompat.BigPictureStyle pictureStyle = new BigPictureStyle();
pictureStyle.setBigContentTitle("BigContentTitle")
.setSummaryText("Summary Text").bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
Notification notification = new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setTicker("showBigView_pic").setContentInfo("ContentInfo")
.setContentTitle("ContentTitle").setContentText("ContentText")
.setStyle(pictureStyle)
.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.build();
manager.notify(3, notification);
}
/**
* 带进度条的通知栏
*/
private void showNotification4() {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("showProgressBar")
.setContentInfo("ContentInfo")
.setOngoing(true)
.setContentTitle("ContentTitle")
.setContentText("ContentText");
new Thread(new Runnable() { @Override
public void run() {
int progress = 0;
for(progress=0;progress<100;progress+=5){
//将第三个参数改为true,进度条会变为无明显进度的样式
builder.setProgress(100, progress, true);
manager.notify(0, builder.build());
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
//线程执行完毕之后,修改通知栏的显示
builder.setContentTitle("Download complete").setProgress(0, 0, false).setOngoing(false);
manager.notify(0, builder.build());
}
}).start(); }
/**
* 大通知栏
*/
private void showNotification3() {
NotificationCompat.BigTextStyle textStyle = new BigTextStyle();
textStyle.setBigContentTitle("BigContentTitle")
.setSummaryText("Summary Text")
.bigText("I'm Big Text......................................................................");
Notification notification = new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Ticker")
.setContentInfo("ContentInfo")
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setStyle(textStyle)
.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL).build();
manager.notify(0, notification);
}
/**
* 小通知栏
*/
private void showNotification2() {
Notification notification = new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("showNormal")
.setContentInfo("ContextInfo")
.setContentTitle("ContentTitle")
.setContentText("ContentText")
.setNumber(45)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.build();
Intent notificationIntent =new Intent(MainActivity.this, SecondActivity.class);
PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent=contentItent;
manager.notify(0, notification);
}
/**
* 在状态栏显示通知
*/
private void showNotification(){
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定义Notification的各种属性
Notification notification =new Notification(R.drawable.ic_launcher,
"系统测试", 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_AUTO_CANCEL;
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, SecondActivity.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开发之Notification学习笔记的更多相关文章

  1. android开发之broadcast学习笔记 分类: android 学习笔记 2015-07-19 16:33 32人阅读 评论(0) 收藏

    android中的广播用的太多了,今天稍微总结一下. 按注册方式分为两种: 1.静态注册广播: 静态注册广播就是在androidManifest.xml文件中注册广播,假设我们要实现这样一个效果,在一 ...

  2. android开发之broadcast学习笔记

    android中的广播用的太多了,今天稍微总结一下. 按注册方式分为两种: 1.静态注册广播: 静态注册广播就是在androidManifest.xml文件中注册广播,假设我们要实现这样一个效果,在一 ...

  3. Android开发之Notification通知

    消息通知使我们很常见的,当收到一条消息的时候,通知栏会显示一条通知: 直接看代码: public class MainActivity extends Activity { private Notif ...

  4. Android开发之Notification的简单使用

    创建Notification    Buider 一个Builder至少包含以下内容: 一个小的icon,用setSmallIcon())方法设置 一个标题,用setContentTitle())方法 ...

  5. IOS开发之Swift学习笔记

    1.因为存储属性要求初始化,我们可以使用lazy修饰符来延迟初始化.

  6. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  7. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  8. android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

    android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序     在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity ...

  9. Android开发之旅3:android架构

    引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWorld Android 开发之旅:HelloWorld项目的目录结构 我们对android有了个大致的了解,知道如何搭建andr ...

随机推荐

  1. ARM920T系统总线时序分析

    一.系统总线时序图 二.分析 第一个时钟周期开始,系统地址总线给出需要访问的存储空间地址. 经过Tacs时间后,片选信号也相应给出,并且锁存当前地址线上地址信息. 再经过Tcso时间后,处理器给出当前 ...

  2. APCS

    arm汇编程序中,R0,R1,R2,R3,R12都是作为中间寄存器,而R4-R11是不能随便使用的,暂时我还不知它们的用途.所以,中间寄存器,在程序运行的开始处与结束的时候值是可以不一样的,也就是说中 ...

  3. linux创建交换分区

    一.SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到S ...

  4. MVC描述对象的类关系图/调用关系图【学习笔记】

  5. HTML5之一HTML5简介

    1.什么是HTML5? HTML5是HTML的新一代标准.以前版本的HTML标准4.01发布于1999. 自1999年以后,web已经有了翻天覆地的变化. 实际上HTML5仍旧是开发中的一个标准.但是 ...

  6. java项目创建和部署

    http://www.cnblogs.com/nexiyi/archive/2012/12/28/2837560.html http://dead-knight.iteye.com/blog/1841 ...

  7. php生成二维码,使用qrcode类库创建

    传说Google有个接口,可以生成二维码,这让我有点鸡冻不已,于是,......(省略1000字).结果,我找到了另外一个方法,是使用一个php类库,该工具叫:qrcode,但下载该工具可真是要人命. ...

  8. 使用astyle格式化代码

    astyle是一款代码格式化工具,它的下载地址是:http://sourceforge.net/projects/astyle 一.基本命令 astyle --style=ansi main.cs ( ...

  9. Qt中连接到同一signal的多个slots的执行顺序问题(4.6以后按连接顺序执行)

    起源 前些天忘记在哪儿讨论过这个问题,今天在csdn又看到有网友问这个问题,而其他网友却无一例外的给出了“无序”这个答案. Manual Qt的问题,当manual中有明确文字说明时,我们应该以Qt的 ...

  10. quartz源码解析--转

    quartz源码解析(一)  . http://ssuupv.blog.163.com/blog//146156722013829111028966/ 任何个人.任何企业.任何行业都会有作业调度的需求 ...