Android应用开发学习之状态栏通知
作者:刘昊昱
博客:http://blog.csdn.net/liuhaoyutz
状态栏通知涉及到两个类,一是Notification,它代表一个通知;另一个是NotificationManager,它是用于发送Notification的系统服务。
使用状态栏通知一般有4个步骤:
1、 通过getSystemService()方法获取NotificationManager服务。
2、 创建一个Notification对象,并为其设置各种属性。
3、 为Notification对象设置事件信息。
4、 通过NotificationManager类的notify()方法将通知发送到状态栏。
下面我们来看一个例子,其运行效果如下所示:
主布局文件main.xml只是放置两个按钮,其内容如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/button1"
android:text="发送消息"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <Button
android:id="@+id/button2"
android:text="清空消息"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
下面看主Activity文件,其内容如下所示:
package com.liuhaoyu; import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity {
final int NOTIFYID_1 = 123; //第一个通知的ID
final int NOTIFYID_2 = 124; //第二个通知的ID /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { Notification notify = new Notification();
notify.icon = R.drawable.image01;
notify.tickerText = "有新通知啦!";
notify.when = System.currentTimeMillis();
notify.defaults = Notification.DEFAULT_ALL;
notify.setLatestEventInfo(MainActivity.this, "通知1", "周六下午5点打篮球", null);
notificationManager.notify(NOTIFYID_1, notify); Notification notify1 = new Notification(R.drawable.image01, "哈哈,又有新通知了", System.currentTimeMillis());
notify1.flags|=Notification.FLAG_AUTO_CANCEL;
Intent intent=new Intent(MainActivity.this,ContentActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
notify1.setLatestEventInfo(MainActivity.this, "通知2",
"点击查看详情", pendingIntent);
notificationManager.notify(NOTIFYID_2, notify1);
}
}); Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// notificationManager.cancel(NOTIFYID_1); //清除ID号为常量NOTIFYID_1的通知
notificationManager.cancelAll(); //清除全部通知 }
});
}
}
当点击第一个按钮时,发送两个通知,第一个直接显示通知内容,第二个通过启动另外一个Activity 即ContentActivity来显示通知内容。
当点击第二个按钮时,清空所有通知,也可以删除指定ID号的通知。
因为第一个通知设置使用默认声音、默认振动及默认闪光灯,即程序需要访问系统闪光灯资源和振动器,所以我们需要在AndroidManifest.xml文件中声明使用权限,加上如下语句:
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.VIBRATE"/>
因为第二个通知要启动另外一个Activity来显示通知内容,所以我们也要在AndroidManifest.xml文件中声明这个Activity,加入如下语句:
<activity
android:label="通知"
android:name=".ContentActivity"
android:theme="@android:style/Theme.Dialog" />
通知2调用的活动ContentActivity比较简单,仅仅是显示一条通知,但注意我们通过在AndroidManifest.xml中的android:theme="@android:style/Theme.Dialog"指定它为对话框的形式。该活动实现如下:
package com.liuhaoyu; import android.app.Activity;
import android.os.Bundle; public class ContentActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
} }
其布局文件内容如下:
<?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="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周日下午5点打羽毛球"
android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
Android应用开发学习之状态栏通知的更多相关文章
- Android应用开发学习之表格视图
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来学习一个使用表格视图的程序,下图是该程序的运行效果: 该程序主Activity文件内容如下: packag ...
- 2021年正确的Android逆向开发学习之路
2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...
- Android应用开发学习笔记之播放音频
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...
- android移动开发学习笔记(二)神奇的Web API
本次分两个大方向去讲解Web Api,1.如何实现Web Api?2.如何Android端如何调用Web Api?对于Web Api是什么?有什么优缺点?为什么用WebApi而不用Webservice ...
- Android成长日记-Noification实现状态栏通知
Notification可以作为状态栏的通知,实现这个效果需要使用NotificationManager实现控制类,才能实现对这个效果的显示 下面是实现状态栏显示效果的通知: 1. 首先在Layout ...
- Android应用开发学习笔记之事件处理
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android提供的事件处理机制分为两类:一是基于监听的事件处理:二是基于回调的事件处理.对于基于监听的事件处理,主 ...
- Android 系统开发学习杂记(转)
http://blog.csdn.net/shagoo/article/details/6709430 > 开发环境1.安装 Eclipse 和 android-sdk 并解压安装2.Eclip ...
- Android应用开发学习之相对布局
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边.右边.上边或下 ...
- Android应用开发学习笔记之AsyncTask
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在上一篇文章中我们学习了多线程和Handler消息处理机制,如果有计算量比较大的任务,可以创建一个新线程执行计算工作 ...
随机推荐
- pkill killall kill pidof
http://blog.csdn.net/whycold/article/details/11771841 http://www.cnblogs.com/rsky/p/4886043.html ht ...
- Html5学习笔记(一)
一:常见标签类型 块级标签 特点:1.独占一行 2,可以随时设置w,h 2.行内标签(内联) 特点: 1.多个行内标签能同时显示在一行 2.w.h取决于内容的尺寸() 3.行内-块级标签 特点 ...
- 企业生产环境中linux系统分区的几种方案
方案1:针对网站集群架构中的某个节点服务器分区 该服务器上的数据有多份(其他节点也有)且数据不太重要,建议分区方案如下: /boot: 200MB swap: 物理内存的1.5倍,当内存大于或等于8G ...
- (转)iOS7界面设计规范(10) - UI基础 - 文字排版与配色
明天就是周四了.貌似前几天还在恨周一呢.话说今天几乎开了一整天的会,正经事情没做多少:这种感觉比一整天从早到晚12个小时的忙碌于一件事情还要让人感到疲惫的对叭?那今天的iOS7设计规范更新又是一篇很简 ...
- 常用的50条linux 命令
从今天起,咱开始正式学习python了,于是遍整理了50条linux的常用命令. 1 线上查询帮助命令 :man 遇到什么不会的命令可以 man +你想要查询的命令 (需要有网),因为是英文的所以 ...
- 监控工具nagios
Nagios 简介是一个开源软件,可以监控网络设备网络流量.Linux/windows主机状态,甚至可以监控打印机它可以运行在Linux上或windows上基于浏览器的web界面方便运维人员查看监控项 ...
- HDU 5903 - Square Distance [ DP ] ( BestCoder Round #87 1002 )
题意: 给一个字符串t ,求与这个序列刚好有m个位置字符不同的由两个相同的串拼接起来的字符串 s, 要求字典序最小的答案 分析: 把字符串折半,分成0 - n/2-1 和 n/2 - n-1 d ...
- hdu 5671 矩阵变换
Matrix Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- HEAP[xxx.exe]:Invalid Address specified to RtlValidateHeap 错误的解决方法总结
一.情况 抽象出问题是这样的: class DLL_API1 A { func() { vector vec; B b; b.func(vec); return TRUE; } } 其中B是另一个导出 ...
- 改变HTML中超链接的显示样式
更详细的内容请参考:http://www.w3school.com.cn/tags/tag_a.asp HTML中的代码如下: <a class="news_title" t ...