Android支持Toast和NotificationManager两种通知方式,前者相当于一个定时关闭的对话框,后者是在状态栏上显示一条消息。Toast和Notification都可以随时取消。

Toast

A toast is a view containing a quick little message for the user. The toast class helps you create and show those. Toast的使用很简单:

Toast.makeText(this, "Service destroyed…", Toast.LENGTH_LONG).show();

NotificationManager

NotificationManager负责通知用户事件的发生。

NotificationManager有三个公共方法:

1.cancel(int id)取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.

2.cancelAll()取消以前显示的所有通知。

3.notify(int id, Notification notification) 把通知持久的发送到状态条上.

//初始化NotificationManager:

NotificationManager nm =

(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification代表着一个通知.

Notification的属性:

audioStreamType 当声音响起时,所用的音频流的类型

contentIntent 当通知条目被点击,就执行这个被设置的Intent.

contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示.

defaults 指定哪个值要被设置成默认的.

deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.

icon 状态条所用的图片.

iconLevel 假如状态条的图片有几个级别,就设置这里.

ledARGBLED灯的颜色.

ledOffMSLED关闭时的闪光时间(以毫秒计算)

ledOnMS LED开始时的闪光时间(以毫秒计算)

number 这个通知代表事件的号码

sound 通知的声音

tickerText通知被显示在状态条时,所显示的信息

vibrate 振动模式.

when 通知的时间戳.

Notification的公共方法:

describeContents()Describe the kinds of special objects contained in this Parcelable's marshalled representation.

setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) 设置Notification留言条的参数

writeToParcel(Parcel parcel, int flags)Flatten this notification from a parcel.

toString() …………….

将Notification发送到状态条上:

Notification notification = new Notification(R.drawable.icon,

"Service started", System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,

new Intent(this, Main.class), 0);

// must set this for content view, or will throw a exception

notification.setLatestEventInfo(this, "Test Service",

"Service started", contentIntent);

nm.notify(R.string.hello, notification);

Notification的取消

nm.cancel(R.string.hello);

package wzhnsc.testnotification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class Main extends Activity {

NotificationManager objNotificationManager;
Notification objNotification;
PendingIntent objPendingIntent;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 声明通知(消息)管理器只要知道它是用来管理通知消息的就行了
objNotificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE );

int icon = R.drawable.icon;
CharSequence tickerText = "小图标旁的文字";
objNotification = new Notification( icon,
tickerText,
System.currentTimeMillis() );

// 如果有振动或者全部提示方式,必须在 AndroidManifest.xml 加入振动权限
objPendingIntent = PendingIntent.getActivity( this,
0,
new Intent( this, Main.class ),
0 );

objNotification.setLatestEventInfo( this,
"通知栏的标题",
"通知栏的内容",
objPendingIntent );

// 添加入通知管理器中
objNotificationManager.notify( 0,
objNotification );
}
}

更多相关文章

  1. android selector 的几种状态
  2. Android(安卓)文字环绕 图文混排 支持Span折叠
  3. 在Android中监控来电和去电
  4. Android中显示输入的隐藏密码/Android多语系支持
  5. Android中的资源分析
  6. Android图形系统的分析与移植--二、Android显示系统软件架构分析
  7. android window类
  8. Android(安卓)P 图像显示系统(三)Android(安卓)HWUI 绘制流程
  9. Android中状态栏适配问题

随机推荐

  1. Android进程保活方法 --转自腾讯Bugly公
  2. Android(安卓)给应用定制皮肤-------值得
  3. Android(安卓)培训计划:如何从功能机时代
  4. [置顶] Android异步更新UI-线程池-Future
  5. 代码中进行RelativeLayout布局的修改添加
  6. Android中的JSON详细总结
  7. Android凭什么斩杀苹果: 硬件繁荣 开源系
  8. [置顶] 关于android安装sdk时找不到jdk的
  9. Android应用性能剖析全攻略
  10. 深入解析:Android热修复技术选择和原理