Android开发之显示通知
Toast类可以用来显示消息给用户,虽然它很方便,但是有不能持久。它只是在屏幕上显示几秒后就自动消失掉了。对于重要的信息或消息,要使用更加持久的方法。这种情形下,就应当使用通知,即使用NotificationManager在设备顶部的状态栏(也叫做通知栏)中显示一条持久化的信息或消息。
要显示一个通知,首先要创建一个指向NotificationView类 Intent对象:
Intent intent = new Intent(this,NotificationViewActivity.class);
intent.putExtra("notificationID", notificationID);
当用户从通知列表中选择一个通知的时候,这个Intent就被用来启动另一个活动。
另外还需要创建一个PendingIntent对象。PendingIntent对象可以代表应用程序帮助您在后面某个时候执行一个动作,而不用考虑应用程序是否正在运行。一般如下初始化:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
getActivity()方法检索一个PendingIntent对象并使如下参数设置它:
上下文-应用程序上下文
请求码-用于意图的请求码
意图-用来启动目标活动的意图
标志-活动启动时使用的标志
然后,获取一个NotificationManager类的实例并创建一个Notification类的实例:
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher,
"Reminder:Meeting stats in 5 minutes",
System.currentTimeMillis());
接下来,使用setLatestEventInfo()方法来设置通知的详细内容:
CharSequence from = "System Alarm";
CharSequence message = "Meeting with cusomer at 3pm..."; notification.setLatestEventInfo(this, from, message, pendingIntent); notification.vibrate = new long[]{100,250,100,500};//设置通知为震动手机形式
nm.notify(notificationID, notification);
当用户点击通知时候,NotificationViewActivity活动就会启动,这里使用NotificationManager对象的cancel()方法来取消这个通知:
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancel(getIntent().getExtras().getInt("notificationID"));
完整实例代码如下:
MainActivity活动类:
package com.example.notifications; import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { private int notificationID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
displayNotification();
}
protected void displayNotification(){
Intent intent = new Intent(this,NotificationViewActivity.class);
intent.putExtra("notificationID", notificationID); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher,
"Reminder:Meeting stats in 5 minutes",
System.currentTimeMillis());
CharSequence from = "System Alarm";
CharSequence message = "Meeting with cusomer at 3pm..."; notification.setLatestEventInfo(this, from, message, pendingIntent); notification.vibrate = new long[]{100,250,100,500};
nm.notify(notificationID, notification); }
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
// } }
NotificationViewActivity活动类:
package com.example.notifications; import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.view.Menu; public class NotificationViewActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_view); NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.cancel(getIntent().getExtras().getInt("notificationID"));
} }
Android开发之显示通知的更多相关文章
- Android 开发——如何显示 GIF 动画
gif 图动画在 android 中还是比较常用的,比如像新浪微博中,有很多 gif 图片,而且展示非常好,所以我也想弄一个.经过我多方的搜索资料和整理,终于弄出来了,其实 github 上有很多开源 ...
- Android开发之显示进度对话框
一般有两种对话框,一个是普通的简单的please wait对话框,另一种是创建显示操作进度(如下载状态)的对话框. 第一种普通的效果图如下: 第一种普通的实现代码: public void onCli ...
- Android开发之漫漫长途 Ⅴ——Activity的显示之ViewRootImpl的PreMeasure、WindowLayout、EndMeasure、Layout、Draw
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android开发——Notification通知的各种Style详解
本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...
- Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API
想要看全部设置的请看这一篇 [转]NotificationCopat.Builder全部设置 常用设置: 设置属性 说明 setAutoCancel(boolean autocancel) 设置点击信 ...
- android开发学习---layout布局、显示单位和如何进行单元测试
一.五大布局(layout) android中的用五大布局:LinearLayout (线性布局).AbsoluteLayout(绝对布局).RelativeLayout(相对布局).TableLay ...
- Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(2)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(1)
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android开发之漫漫长途 Ⅳ——Activity的显示之ViewRootImpl初探
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
随机推荐
- linux下使用NFS挂载文件系统
转自linux如何使用NFS挂载文件系统 设备:一台服务器和一台客户端,这里我们把装在PC机上的RedHat作为服务器,而客户端则是嵌入式linux开发板. 环境:开发板已启动,连接好串口和网线,串口 ...
- Android java程序获取assets资产文件
AssetManager assetManager=this.getAssets(); inputStream = assetManager.open("test.xml");
- Fibonacci数列对任何数取模都是一个周期数列
题目是要求出斐波那契数列n项对一个正整数取模,那么可以把斐波那契数列取模后得到的数列周期求出来. 比如下面一个题目:求出f[n]的后4位,先求出数列对10000取模的周期,然后再查找即可. #incl ...
- windows进程中的内存结构(好多API,而且VC最聪明)
在阅读本文之前,如果你连堆栈是什么多不知道的话,请先阅读文章后面的基础知识. 接触过编程的人都知道,高级语言都能通过变量名来访问内存中的数据.那么这些变量在内存中是如何存放的呢?程序又是如何使用这 ...
- [LeetCode] Super Ugly Number (Medium)
Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...
- 【HDOJ】4553 约会安排
线段树.线段树的细节很重要,小数据遍历可以发现问题. /* 4553 */ #include <iostream> #include <string> #include < ...
- POJ_3045_Cow_Acrobats_(贪心)
描述 http://poj.org/problem?id=3045 n头牛,每头牛都有重量w[i]和力量s[i].把这n头牛落起来,每头牛会有一个危险值,危险值是它上面所有牛的重量和减去它的力量.求危 ...
- Eclipse超级完美汉化教程
转自:http://jingyan.baidu.com/article/e75057f28401a8ebc91a899e.html 是中国人都喜欢汉化的东西,除非你想挑战英文,抑或你就是英语高手.百度 ...
- lfs遇到的一些问题--准备阶段
本机宿主系统archlinux,lfs SVN-20130711,参考文档 1.在离开或重新进入当前工作环境 (比如 su 成为 root 或者其他用户) 时不要忘记检查 $LFS 是否设置好. ec ...
- Bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 dijkstra,堆,分层图
1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1573 Solv ...