Android的状态栏通知(Notification)
通知用于在状态栏显示消息,消息到来时以图标方式表示,如下:
如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息.
1、Layout布局文件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="46dp"
android:onClick="test1"
android:text="@string/text_notifi" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="94dp"
android:onClick="clearNoti"
android:text="@string/text_clear" /> </RelativeLayout>
2、string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">lession16-notifi</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> <string name="text_notifi">Notification应用案例</string>
<string name="text_clear">清除通知</string> </resources>
3、MainActivity
package com.example.lession16_notifi; import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity {
private NotificationManager notificationManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @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;
} public void test1(View v){
showNotification("来短信了", "5557", "I love you", R.drawable.ic_launcher, R.drawable.ic_launcher);
} public void showNotification(String tickerText,String contentTitle,String contentText,int iconId,int notiId){
//创建一个Notification
Notification notification=new Notification();
//设置通知 消息 图标
notification.icon=iconId;
//设置发出消息的内容
notification.tickerText=tickerText;
//设置发出通知的时间
notification.when=System.currentTimeMillis(); //设置显示通知时的默认的发声、震动、Light效果
notification.defaults=Notification.DEFAULT_VIBRATE;//震动
//Notification notification = new Notification(R.drawable.ic_launcher, "有新的消息", System.currentTimeMillis()); //3步:PendingIntent android系统负责维护
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, getIntent(), 0);
//4步: 设置更加详细的信息
notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
//5步:使用notificationManager对象的notify方法 显示Notification消息 需要制定 Notification的标识
notificationManager.notify(notiId, notification);
} public void clearNoti(View v){
notificationManager.cancelAll();//清除所有
} }
注:模拟器要实现震动需要加权限:DEFAULT_VIBRATE
Android的状态栏通知(Notification)的更多相关文章
- 【Android】状态栏通知Notification、NotificationManager详解(转)
在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类: NotificationMa ...
- Android 状态栏通知Notification、NotificationManager简介
Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...
- 状态栏通知Notification的简单使用
今天在学习Notification时同时参考了一些相关的博客,现在结合自身学习实际来总结一下. 在使用手机时,当有未接来电或者短消息时,通常会在手机屏幕上的状态栏上显示.而在Android中有提醒功能 ...
- Android 状态栏通知 Notification
private NotificationManager manager; private Notification.Builder builder; @Override protected void ...
- Android 状态栏通知Notification、NotificationManager详解
http://www.cnblogs.com/onlyinweb/archive/2012/09/03/2668381.html
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Android推送通知Notification
参考: 1.http://www.cnblogs.com/anrainie/archive/2012/03/07/2383941.html 总结: 代码:
- 安卓状态栏通知Status Bar Notification
安卓系统通知用户三种方式: 1.Toast Notification 2.Dialog Notification 3.Status Bar Notification Status Bar Notifi ...
- Android学习总结(十五) ———— Notification(状态栏通知)基本用法
一.Notification基本概念 Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...
随机推荐
- Thrift实践
Thrift实践:(一)安装 -- 未完待续 1. 新建一个目录,C:\test\thrift-test,里面建2个子文件夹,client-node和sever-csharp,然后把Thrift官 ...
- SQL点滴30—SQL中常用的函数
原文:SQL点滴30-SQL中常用的函数 该文章转载自http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 别人的总结,很 ...
- avalonjs 1.3.7发布
avalonjs 1.3.7发布 又到每个月的15号了,现在avalon已经固定在每个月的15号发布新版本.这次发布又带来许多新特性,让大家写码更加轻松,借助于“操作数据即操作DOM”的核心理念与双向 ...
- 九度OJ 1035:找出直系亲属(二叉树)
题目1035:找出直系亲属 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1309 解决:521 题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如 ...
- postal邮件发送(一):基本配置
前言 源码:https://github.com/andrewdavey/postal 文档:http://aboutcode.net/postal/ postal最大好处就是再也不用拼接html发送 ...
- Oracle表介绍--簇表
簇和簇表 簇其实就是一组表,是一组共享相同数据块的多个表组成. 将经常一起使用的表组合在一起成簇可以提高处理效率. 在一个簇中的表就叫做簇表.建立顺序是:簇→簇表→数据→簇索引 ...
- james+javamail入门
James+Javamail构建邮件服务(一) 本文描述如何使用James搭建具备一定邮件过滤.邮件操作功能的邮件服务器,以及使用Javamail实现对James服务器邮件的收发功能. 1关于Jame ...
- .NET MVC4 实训记录之六(利用ModelMetadata实现资源的自主访问)
上一篇我们已经实现自定义资源文件的访问,该篇我们使用它配合ModelMetadata实现资源文件的自主访问.这样做是为了我们能更简单的用MVC原生的方式使用资源文件.由于我的文章旨在记录MVC项目的实 ...
- Visual Studio 2013 IIS Explorer 停止调试继续访问站点
升级到2013后,在做调试的时候默认调试服务器是 IIS Explorer,当终止调试的时候再次访问调试站点时已经无法访问了.此时想预览一下感觉很不方便. 为了能够预览可以参考一下配置: Tools ...
- [翻译]用Dart塑造Android未来
明天回家,今天下午瞅时间翻译了Cyril Mottier的另外一篇有关Android前景的文章. 原谅地址是:http://cyrilmottier.com/2014/06/12/shaping-th ...