标签:android notification
下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个快讯。已添加的Notification.Builder,使其更容易构建通知。notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。
先来区分以下状态栏和状态条的区别:
1、状态条就是手机屏幕最上方的一个条形状的区域;
在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;
2、状态栏就是手从状态条滑下来的可以伸缩的view;
在状态栏中一般有两类(使用FLAG_标记):
(1)正在进行的程序;
(2)是通知事件;
快速创建一个Notification的步骤简单可以分为以下四步:
第一步:通过getSystemService()方法得到NotificationManager对象;
-
nManager = (NotificationManager) this.getSystemService(service);
第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;
-
notification.icon = R.drawable.ic_launcher;
-
notification.tickerText = tickerText;
-
notification.when = when;
-
notification.sound = Uri.parse("android.resource://com.sun.alex/raw/dida"); // 自定义声音
-
notification.flags = Notification.FLAG_NO_CLEAR;
-
notification.flags = Notification.FLAG_ONGOING_EVENT;
-
notification.flags |= Notification.FLAG_AUTO_CANCEL;
-
notification.flags |= Notification.FLAG_INSISTENT;
-
notification.defaults = Notification.DEFAULT_SOUND;
-
notification.defaults = Notification.DEFAULT_VIBRATE;
-
notification.defaults = Notification.DEFAULT_ALL;
-
notification.defaults = Notification.DEFAULT_ALL;
第三步:通过NotificationManager对象的notify()方法来执行一个notification的消息;
-
nManager.notify(ID, notification);
第四步:通过NotificationManager对象的cancel()方法来取消一个notificatioin的消息;
Notification.build构造Notification方法介绍:
void setLatestEventInfo(Context context , CharSequencecontentTitle,CharSequence contentText,PendingIntent contentIntent)
功能: 显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象
参数: context 上下文环境
contentTitle 状态栏中的大标题
contentText 状态栏中的小标题
contentIntent 点击后将发送PendingIntent对象
说明:要是在Notification中加入图标,在状态栏和状态条中显示图标一定要用这个方法,否则报错!
NotificationManager类的常用方法:
通过获取系统服务来获取该对象:
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;
NotificationManager常用方法介绍:
public void cancelAll() 移除所有通知 (只是针对当前Context下的Notification)
public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public void notify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为id
public void notify(int id, Notification notification) 将通知加入状态栏,,标记为id
-
package com.sun.alex;
-
-
import android.app.Activity;
-
import android.app.Notification;
-
import android.app.NotificationManager;
-
import android.app.PendingIntent;
-
import android.content.Intent;
-
import android.net.Uri;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public class NotificationActivity extends Activity {
-
-
private Button sendBtn, cancelBtn;
-
private Notification notification;
-
private NotificationManager nManager;
-
private Intent intent;
-
private PendingIntent pIntent;
-
-
private static final int ID = 1;
-
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
sendBtn = (Button) this.findViewById(R.id.send);
-
cancelBtn = (Button) this.findViewById(R.id.cancel);
-
-
String service = NOTIFICATION_SERVICE;
-
nManager = (NotificationManager) this.getSystemService(service);
-
-
notification = new Notification();
-
String tickerText = "测试Notifaction";
-
-
long when = System.currentTimeMillis();
-
-
notification.icon = R.drawable.ic_launcher;
-
notification.tickerText = tickerText;
-
notification.when = when;
-
notification.sound = Uri.parse("android.resource://com.sun.alex/raw/dida"); // 自定义声音
-
notification.flags = Notification.FLAG_NO_CLEAR;
-
notification.flags = Notification.FLAG_ONGOING_EVENT;
-
notification.flags |= Notification.FLAG_AUTO_CANCEL;
-
notification.flags |= Notification.FLAG_INSISTENT;
-
notification.defaults = Notification.DEFAULT_SOUND;
-
notification.defaults = Notification.DEFAULT_SOUND;
-
notification.defaults = Notification.DEFAULT_VIBRATE;
-
notification.defaults = Notification.DEFAULT_ALL;
-
notification.defaults = Notification.DEFAULT_ALL;
-
-
sendBtn.setOnClickListener(sendClickListener);
-
cancelBtn.setOnClickListener(cancelClickListener);
-
}
-
-
private OnClickListener sendClickListener = new OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
-
intent = new Intent(NotificationActivity.this,
-
NotificationResult.class);
-
-
pIntent = PendingIntent.getActivity(NotificationActivity.this, 0,
-
intent, 0);
-
-
notification.setLatestEventInfo(NotificationActivity.this, "标题",
-
"内容", pIntent);
-
-
nManager.notify(ID, notification);
-
}
-
};
-
-
private OnClickListener cancelClickListener = new OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
-
nManager.cancel(ID);
-
}
-
};
-
}
Android中的消息通知(NotificationManager和Notification)
标签:android notification
原文地址:http://blog.csdn.net/u014608640/article/details/42461171