Android通知栏学习(基础篇)

Android通知栏学习

基础方法

步骤一:NotificationManager

状态栏通知的管理类,负责发通知、清除通知等操作

//实例化方法

NotificationManager mNotificationManager  = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  

几个方法:

// 清除通知,就是把存在通知栏的通知去掉

cancel(int id)

cancel(String tag, int id)

cancelAll()

// 在状态栏显示一个通知

notify(int id, Notification notification)

notify(String tag, int id, Notification notification)

步骤二:Notification.Builder

通知构造类,主要方便对通知属性的各种设置(API level 4)

实例化方法

Notification.Builder mBuilder = new new Notification.Builder(mContext);

常用方法如下:


// 设置通知标题 
// Set the first line of text in the platform notification template.
setContentTitle(CharSequence title)

// 设置通知的文字  
// Set the second line of text in the platform notification template.
setContentText(CharSequence text)

// 设置通知提示信息,就是状态栏提示时到文字
// Set the "ticker" text which is displayed in the status bar when the notification
// first arrives.
setTicker(CharSequence tickerText)

// 设置通知的图片
// Set the small icon resource, which will be used to represent the notification in
// the status bar.  
setSmallIcon(int icon)

// 用户点击通知栏时自动退出通知栏,个人经验需要与setContentIntent (PendingIntent intent)
// 配合使用才能看到效果
// Make this notification automatically dismissed when the user touches it.
setAutoCancel (boolean autoCancel)

// 可以用来设置点击跳转意图
// Supply a PendingIntent to be sent when the notification is clicked.
setContentIntent (PendingIntent intent)

// 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
// Add a timestamp pertaining to the notification (usually the time 
// the event occurred).
setWhen (long when)
特殊用法
// 当用户取消通知栏时,此意图有效
setDeleteIntent (PendingIntent intent)

步骤三:Notification

这个就是通知类了

最简单的实例化方法

Notification mNotification = mBuilder.build();

步骤四:最后一步,发送通知请求

直接一句代码搞定

mNotificationManager.notify(notifyId, mBuilder.build());  

//或者

mNotificationManager.notify(notifyTag,notifyId, mBuilder.build());  

就是NotificationManager的方法,在上面又提到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文字滚动-Java文字走马灯,这是大家熟悉的跑马灯特效,文字依次滚动,不停的循环滚动,就类似网页上的文字滚动效果,本例的文字由下到上滚动显示。// 文字跑马灯与信息窗口   public class MessageDemo extends MIDlet implements CommandListener {   private Display display; // 设备的显示器   private Form form; // 表单   private Command cancel; // 取消命令   private String message="随着越来越多手提电话和个人数字助理开始融入到信息高速公路之上,从移动设备上访问Web站点变得越来越重要。Java开创了消费设备中小型的储存容量的先河,它是用于开发手机、传呼机及其他微型设备应用程序的理想语言。";   private StringItem messageItem; //信息显示Item   public MessageDemo() {    form = new Form("信息显示");    cancel = new Command("取消", Command.CANCEL, 1);    messageItem=new StringItem("",message);   }   // 重载抽象类MIDlet的抽象方法startApp()   protected void startApp() {    display = Display.getDisplay(this); //取得设备的显示器    Ticker ticker = new Ticker("使用J2ME设计无线网络应用程序"); // 创建滚动条    form.setTicker(ticker); // 把滚动条加到表单上    messageItem.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_LARGE)); //设置显示字体    form.append(messageItem); // 把显示信息添加到表单上    form.addCommand(cancel); // 为表单加上取消命令    form.setCommandListener(this); // 为表单设置命令监听器    display.setCurrent(form); // 显示表单   }   // 重载抽象类MIDlet的抽象方法pauseApp()   protected void pauseApp() {   }   // 重载抽象类MIDlet的抽象方法destroyApp()   protected void destroyApp(boolean u) {    notifyDestroyed();   }   // 实现接口CommandListener的方法   public void commandAction(Command c, Displayable d) {    if (c == cancel) {    destroyApp(false); // 销毁程序    notifyDestroyed();    }   }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值