Android之Notification的多种用法
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的。
我们也知道Android系统也是在不断升级的,有关Notification的用法也就有很多种,有的方法已经被android抛弃了,现在我实现了三 种不同的方法,并适应不同的android版本。现在我就把代码公布出来,我喜欢把解释写在代码中,在这里我就不多说了,先看效果图:
再看代码,主要的代码如下:
- package net.loonggg.notification;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.RemoteViews;
- public class MainActivity extends Activity {
- private static final int NOTIFICATION_FLAG = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void notificationMethod(View view) {
- // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。
- NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- switch (view.getId()) {
- // 默认通知
- case R.id.btn1:
- // 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
- new Intent(this, MainActivity.class), 0);
- // 下面需兼容Android 2.x版本是的处理方式
- // Notification notify1 = new Notification(R.drawable.message,
- // "TickerText:" + "您有新短消息,请注意查收!", System.currentTimeMillis());
- Notification notify1 = new Notification();
- notify1.icon = R.drawable.message;
- notify1.tickerText = "TickerText:您有新短消息,请注意查收!";
- notify1.when = System.currentTimeMillis();
- notify1.setLatestEventInfo(this, "Notification Title",
- "This is the notification message", pendingIntent);
- notify1.number = 1;
- notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
- // 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示
- manager.notify(NOTIFICATION_FLAG, notify1);
- break;
- // 默认通知 API11及之后可用
- case R.id.btn2:
- PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,
- new Intent(this, MainActivity.class), 0);
- // 通过Notification.Builder来创建通知,注意API Level
- // API11之后才支持
- Notification notify2 = new Notification.Builder(this)
- .setSmallIcon(R.drawable.message) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap
- // icon)
- .setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在status
- // bar上显示的提示文字
- .setContentTitle("Notification Title")// 设置在下拉status
- // bar后Activity,本例子中的NotififyMessage的TextView中显示的标题
- .setContentText("This is the notification message")// TextView中显示的详细内容
- .setContentIntent(pendingIntent2) // 关联PendingIntent
- .setNumber(1) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。
- .getNotification(); // 需要注意build()是在API level
- // 16及之后增加的,在API11中可以使用getNotificatin()来代替
- notify2.flags |= Notification.FLAG_AUTO_CANCEL;
- manager.notify(NOTIFICATION_FLAG, notify2);
- break;
- // 默认通知 API16及之后可用
- case R.id.btn3:
- PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,
- new Intent(this, MainActivity.class), 0);
- // 通过Notification.Builder来创建通知,注意API Level
- // API16之后才支持
- Notification notify3 = new Notification.Builder(this)
- .setSmallIcon(R.drawable.message)
- .setTicker("TickerText:" + "您有新短消息,请注意查收!")
- .setContentTitle("Notification Title")
- .setContentText("This is the notification message")
- .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API
- // level16及之后增加的,API11可以使用getNotificatin()来替代
- notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
- manager.notify(NOTIFICATION_FLAG, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示
- break;
- // 自定义通知
- case R.id.btn4:
- // Notification myNotify = new Notification(R.drawable.message,
- // "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());
- Notification myNotify = new Notification();
- myNotify.icon = R.drawable.message;
- myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";
- myNotify.when = System.currentTimeMillis();
- myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
- RemoteViews rv = new RemoteViews(getPackageName(),
- R.layout.my_notification);
- rv.setTextViewText(R.id.text_content, "hello wrold!");
- myNotify.contentView = rv;
- Intent intent = new Intent(Intent.ACTION_MAIN);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
- intent, 1);
- myNotify.contentIntent = contentIntent;
- manager.notify(NOTIFICATION_FLAG, myNotify);
- break;
- case R.id.btn5:
- // 清除id为NOTIFICATION_FLAG的通知
- manager.cancel(NOTIFICATION_FLAG);
- // 清除所有的通知
- // manager.cancelAll();
- break;
- default:
- break;
- }
- }
- }
再看主布局文件:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/btn1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="notificationMethod"
- android:text="默认通知(已被抛弃,但是通用)" />
- <Button
- android:id="@+id/btn2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="notificationMethod"
- android:text="默认通知(API11之后可用)" />
- <Button
- android:id="@+id/btn3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="notificationMethod"
- android:text="默认通知(API16之后可用)" />
- <Button
- android:id="@+id/btn4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="notificationMethod"
- android:text="自定义通知" />
- <Button
- android:id="@+id/btn5"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="notificationMethod"
- android:text="清除通知" />
- </LinearLayout>
还有一个是:自定义通知的布局文件my_notification.xml,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#ffffff"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/text_content"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20sp" />
- </LinearLayout>
Android之Notification的多种用法的更多相关文章
- Android之Notification的多种用法(转)
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- android通知-Notification
android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...
- Notification的基本用法以及使用RemoteView实现自定义布局
Notification的作用 Notification是一种全局效果的通知,在系统的通知栏中显示.既然作为通知,其基本作用有: 显示接收到短消息.即时信息等 显示客户端的推送(广告.优惠.新闻等) ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你全然了解它)
在android的应用层中,涉及到非常多应用框架.比如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架.通知机制,ActionBar框架等等. ...
- 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- Android View.setId(int id) 用法
Android View.setId(int id) 用法 当要在代码中动态的添加View并且为其设置id时,如果直接用一个int值时,Studio会警告. 经过查询,动态设置id的方法有两种; 1. ...
- Android之Notification介绍
Notification就是在桌面的状态通知栏.这主要涉及三个主要类: Notification:设置通知的各个属性. NotificationManager:负责发送通知和取消通知 Notifica ...
- Android Intent的几种用法全面总结
Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...
随机推荐
- iOS开发常用的第三方框架
1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS.Mac OS X网络通信类库,现在是G ...
- jsp <%! %> 与 <% %> 区别
转自huangqiqing123.iteye.com/blog/1922014 <body> <%! //1.可定义方法 public String outMethod(){ ret ...
- 关于#include后面<>和" "的区别
1.以尖括号制定头文件,如下所示: #include <stdio.h> 用尖括号来制定文件时,预处理器是以特定的方式来寻找文件,一般是环境中或编译器命令行指定的某种寻找路径.这种设置寻找 ...
- USACO5.4-TeleCowmunication
题目大意:给出一个无向图,要求删除尽量少的点,使给定的2点间不再连通,并输出字典序最小的方案题型:图论-网络流此题难点在于建图,后面就是套网络流的模板.将点看成边,例如第i个点可以看成一条有向边< ...
- 导出页面文档(只在IE8下测试过)
之前说过一篇关于打印的方法,就顺便也看了一下导出,但是该方法需要用户更改浏览器的安全级别设置,因此并不十分推荐,大家如真有需要可以参考一下ZeroClipboard这款插件,我有时间也会去学习一下并贴 ...
- Yii2的相关学习记录,初始化Yii2(二)
前面已经将Yii2下载下来了,那我们就需要能实际的使用. 一.初始化,因为我都是在windows系统下,所以用cmd命令打开下载下来的Yii2的根目录.然后运行下面命令: init 会提示选择0为开发 ...
- Hadoop-CDH5.7.0 for CentOS7
一.需求 系统 CentOS 7 最小化安装 JDK环境 JDK版本:1.8.0_91 jdk-8u91-linux-x64.rpm 下载地址:http://www.oracle.com/techne ...
- 欧几里德算法gcd及其拓展终极解释
这个困扰了自己好久,终于找到了解释,还有自己改动了一点点,耐心看完一定能加深理解 扩展欧几里德算法-求解不定方程,线性同余方程. 设过s步后两青蛙相遇,则必满足以下等式: (x+m*s)-(y+n ...
- MVC中的区域
authour: chenboyi updatetime: 2015-05-03 08:26:30 friendly link: 目录: 1,思维导图 2,AreaRegistration类的Re ...
- 通过Web Deploy方式部署WCF
如何发布WCF, 其实它有很多种方式去发布WCF服务到IIS上,这篇文章将介绍通过Web Deploy的发布方式去部署. 步骤: 在IIS上创建一个网站 打开IIS, 右击“Site” -> & ...