非常久没有使用Android的通知功能了,今天把两年前的代码搬出来一看。发现非常多方法都废弃了,代码中各种删除线看的十分不爽。于是乎,打开Google,查看官方文档。学习最新的发送通知栏消息的方法。

本文中的代码均參照谷歌官方文档编写:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

1.首先。获取系统的通知服务:

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

2.发送一个最简单的通知

    public void simpleNotice(View view) {
        //此Builder为android.support.v4.app.NotificationCompat.Builder中的,下同。
        Builder mBuilder = new Builder(this);
//系统收到通知时。通知栏上面显示的文字。 mBuilder.setTicker("天津。晴,2~15度,微风");
//显示在通知栏上的小图标
mBuilder.setSmallIcon(R.drawable.consult_answer);
//通知标题
mBuilder.setContentTitle("天气预报");
//通知内容
mBuilder.setContentText("天津,晴,2~15度。微风"); //设置大图标,即通知条上左側的图片(假设仅仅设置了小图标,则此处会显示小图标)
mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.share_sina));
//显示在小图标左側的数字
mBuilder.setNumber(6); //设置为不可清除模式
mBuilder.setOngoing(true); //显示通知,id必须不反复,否则新的通知会覆盖旧的通知(利用这一特性。能够对通知进行更新)
nm.notify(1, mBuilder.build());
}

3.删除一个通知。參数即为通知的id

nm.cancel(1);

4.发送一个通知。点击通知后跳转到一个Activity,从这个Activity返回后,进入程序内的某一个页面(一般为主页)

//点击通知进入一个Activity,点击返回时进入指定页面。
public void resultActivityBackApp(View view) {
Builder mBuilder = new Builder(this);
mBuilder.setTicker("通知标题2");
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("通知标题2");
mBuilder.setContentText("点击通知进入一个Activity,点击返回时进入指定页面。 "); //设置点击一次后消失(假设没有点击事件。则该方法无效。 )
mBuilder.setAutoCancel(true); //点击通知之后须要跳转的页面
Intent resultIntent = new Intent(this, ResultActivityBackApp.class); //使用TaskStackBuilder为“通知页面”设置返回关系
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
//为点击通知后打开的页面设定 返回 页面。(在manifest中指定)
stackBuilder.addParentStack(ResultActivityBackApp.class);
stackBuilder.addNextIntent(resultIntent); PendingIntent pIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pIntent);
// mId allows you to update the notification later on.
nm.notify(2, mBuilder.build());
}

同一时候。须要在manifest中为点击通知后打开的Activity指定父Activity.

<activity
android:name=".ResultActivityBackApp"
android:parentActivityName=".MainActivity"> <meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" /> </activity>

(当中,activity的属性parentActivityName为API 16中的属性。meta-data中的代码为兼容API 16下面。

因此,对于大多数程序。这两个地方都得写。)

5.和上述4类似,仅仅是在打开的Activity中返回时回到home页

 //点击通知进入一个Activity,点击返回时回到桌面
public void resultActivityBackHome(View view) {
Builder mBuilder = new Builder(this);
mBuilder.setTicker("通知标题3");
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("通知标题3");
mBuilder.setContentText("点击通知进入一个Activity,点击返回时回到桌面"); //设置点击一次后消失(假设没有点击事件,则该方法无效。)
mBuilder.setAutoCancel(true); Intent notifyIntent = new Intent(this, ResultActivityBackHome.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pIntent); nm.notify(3, mBuilder.build());
}

6.带进度条的通知

     public void progressNotice(View view) {
final Builder mBuilder = new Builder(this);
mBuilder.setTicker("通知标题4"); mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_launcher); // Start a lengthy operation in a background thread
new Thread(new Runnable() {
@Override
public void run() {
int progress;
for (progress = 0; progress <= 100; progress++) {
// Sets the progress indicator to a max value, the current completion percentage,
// and "determinate" state
mBuilder.setProgress(100, progress, false); //不明白进度的进度条
// mBuilder.setProgress(0, 0, true); nm.notify(4, mBuilder.build());
// 模拟延时
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
} // When the loop is finished, updates the notification
mBuilder.setContentText("Download complete");
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
nm.notify(4, mBuilder.build());
}
}
).start();
}

7.扩展布局的通知。按住通知条下滑,能够查看更具体的内容

public void expandLayoutNotice(View view) {
Builder mBuilder = new Builder(this);
mBuilder.setTicker("通知标题5");
mBuilder.setSmallIcon(R.drawable.ic_launcher);
mBuilder.setContentTitle("通知标题5");
mBuilder.setContentText("按住通知下拉可显示扩展布局"); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[]{"Beijing", "Tianjin", "Shanghai", "Guangzhou"};
// 设置扩展布局的标题
inboxStyle.setBigContentTitle("Event tracker details:"); for (String s : events) {
inboxStyle.addLine(s);
}
mBuilder.setStyle(inboxStyle); nm.notify(5, mBuilder.build());
}

8.自己定义布局的通知栏。(依据谷歌的官方文档不推荐这么做。由于使用这样的方式时,对不同屏幕进行适配须要考虑的因素太多。

并且,通知栏应该展示的就是最简明扼要的信息。对于大多数程序默认的布局已经足够了。

//自己定义布局的通知
public void customLayoutNotice(View view) {
Builder mBuilder = new Builder(this);
mBuilder.setTicker("通知标题6");
mBuilder.setTicker("通知标题6");
mBuilder.setSmallIcon(R.drawable.ic_launcher); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_layout_notice);
mBuilder.setContent(remoteViews);
//为RemoteViews上的按钮设置文字
remoteViews.setCharSequence(R.id.custom_layout_button1, "setText", "Button1");
remoteViews.setCharSequence(R.id.custom_layout_button2, "setText", "Button2"); //为RemoteViews上的按钮设置点击事件
Intent intent1 = new Intent(this, CustomLayoutResultActivity.class);
intent1.putExtra("content", "From button1 click!");
PendingIntent pIntentButton1 = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.custom_layout_button1, pIntentButton1); Intent intent2 = new Intent(this, CustomLayoutResultActivity.class);
intent2.putExtra("content", "From button2 click!");
PendingIntent pIntentButton2 = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.custom_layout_button2, pIntentButton2); nm.notify(6, mBuilder.build());
}

Android:通知栏的使用的更多相关文章

  1. android通知栏Notification点击,取消,清除响应事件

    主要是检测android通知栏的三种状态的响应事件 这次在实现推送需求的时候,要用到android通知栏Notification点击后进入消息页面,因为要实现一个保存推送用户名字的功能,我在点击后处理 ...

  2. Android --通知栏Notification

    参考博客:Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它) //创建一个通知栏的Builder构造类 (Create a Notification Bui ...

  3. Android通知栏介绍与适配总结(上篇)

    此文已由作者黎星授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 由于历史原因,Android在发布之初对通知栏Notification的设计相当简单,而如今面对各式各样的通知 ...

  4. Android通知栏介绍与适配总结

    由于历史原因,Android在发布之初对通知栏Notification的设计相当简单,而如今面对各式各样的通知栏玩法,谷歌也不得不对其进行更新迭代调整,增加新功能的同时,也在不断地改变样式,试图迎合更 ...

  5. Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  6. Android 通知栏用法例子

    当程序意外退出时,可以去掉通知栏上显示的图标 1.创建TestNotificationActivity activity类, package com.notioni.test.notification ...

  7. Android 通知栏系列....

    转:http://blog.csdn.net/vipzjyno1/article/details/25248021 在android的应用层中,涉及到很多应用框架,例如:Service框架,Activ ...

  8. Android抽屉(SlidingDrawer --类似android通知栏下拉效果)

    Android抽屉(SlidingDrawer)的实现发 - 红黑联盟http://www.2cto.com/kf/201301/182507.html 可动态布局的Android抽屉之基础http: ...

  9. Android 通知栏Notification的整合 全面学习 (一个DEMO让你全然了解它)

    在android的应用层中,涉及到非常多应用框架.比如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架.通知机制,ActionBar框架等等. ...

  10. 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

随机推荐

  1. WinForm----DataGridview---连接数据库,以及双击一条数据,显示信息到Label控件,也可以是TextBox控件。

    最终效果: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  2. SQL Server 基础 03 查询数据基础

    查询数据 简单的查询 create table stu_info ( sno int not null ,sname ) not null ,sex ) not null ,birth ) not n ...

  3. 目录 of 2013-2014-1(内容已更新结束)

    (内容已更新结束) UML部分: ---------------1.概述2.用例图3.类图4.顺序图 MVC部分: ----------------1.概述2.路由3.控制器4.视图5.模型6.安装部 ...

  4. [置顶] 小白学习KM算法详细总结--附上模板题hdu2255

    KM算法是基于匈牙利算法求最大或最小权值的完备匹配 关于KM不知道看了多久,每次都不能完全理解,今天花了很久的时间做个总结,归纳以及结合别人的总结给出自己的理解,希望自己以后来看能一目了然,也希望对刚 ...

  5. 【COCOS2D-HTML5 开发之三】演示样例项目附源代码及执行的GIF效果图

    本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi) 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/cocos2d- ...

  6. 不用SWIG,Go使用C++代码的方式

    将C++代码用C作一次封装,就可以让Go调用了. 这是一个C++头文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #ifndef CGO_CPPGO_C ...

  7. 分享最新15个加速 Web 开发的框架和工具(梦想天空)

    我们为开发人员挑选了15个最新的  Web 开发框架,你肯定尝试一下这些新鲜的框架,有的可能略微复杂,有的提供了很多的配置选项,也有一些窗口小部件和界面交互的选择.他们将帮助你创建更优秀的网站,提供给 ...

  8. salon_百度百科

    salon_百度百科     salon    编辑    是法语Salon一字的译音,中文意即客厅,原指法国上层人物住宅中的豪华会客厅.从十七世纪,巴黎的名人(多半是名媛贵妇)常把客厅变成著名的社交 ...

  9. Winform - 全屏显示

    //全屏显示 this.WindowState = FormWindowState.Maximized;

  10. 【C语言】超大数乘法运算

    昨天做排列组合的时候遇到A(a,b)这个问题,需要计算A(20,20)超大,计算机32位的,最大数只能是2^32,这让我很悲伤! 于是乎就自己研究了如何进行超大数的计算! /************* ...