Android入门笔记之状态栏通知

<1>简介

Android系统的状态栏(Status Bar)中有一个创新UI设计,这就是可以下拉的通知提示。当系统有一些消息要通知用户时,例如,收到短信、电子邮件、有未接来电时,都会把信息作为通知(Notification)发送给用户。

Status Bar 增加了一个图标到系统状态栏中,还有文本信息(可以不选),增加Notification信息到Notification窗口。你还可以安装Notification利用的声音,震动,设备上闪关灯来提醒用户。 

Notification与Toast都可以起到通知、提醒的作用。但它们的实现原理和表现形式却完全不一样。

Toast其实相当于一个组件(Widget)。有些类似于没有按钮的对话框。而Notification是显示在屏幕上方状态栏中的信息。

Notification需要用NotificationManager来管理,而Toast只需要简单地创建Toast对象即可。

 

<2>关键步骤

Noitficion的构造函数已经过时。      The Notification.Builder hasbeen added to make it easier to construct Notifications.

 

创建 status bar notification:

1.NotificationManager得到一个参考:

NotificationManager mNotificationManager  = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

 

2.创建一个Notification对象。每一个Notification对应一个Notification对象。在这一步需要设置显示在屏幕上方状态栏的通知消息、通知消息前方的图像资源ID和发出通知的时间。

       int icon = R.drawable.notification_icon; //显示在屏幕上方状态栏的图标  

CharSequence tickerText = "Hello";     //显示在屏幕上方状态栏的通知消息  

long when = System.currentTimeMillis(); //发出通知的时间  

Notification notification = new Notification(icon, tickerText, when);  

       3.定义的信息和PendingIntent通知:

PendingIntent封装了一个Intent,当用户点击通知后会跳到对应的Activity。

PendingIntent对象由Android系统负责维护,因此,在应用程序关闭后,该对象仍然不会被释放。 

<3>出现的问题

       Noitficion有些方法已经不被支持,使用Noitficion.builder来创建,api最低要求是16。

<4>代码及解释

      

StatusBarActivity.java:

package com.ui.status;

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 com.activity.firstActivity;
import com.test.R;

public class StatusBarActivity extends Activity{
	/**   
	 * @ProjectName:  [androidtest] 
	 * @Package:      [com.ui.status.StatusBarActivity.java]  
	 * @ClassName:    [StatusBarActivity]   
	 * @Description:    
	 * @Author:       [gmj]   
	 * @CreateDate:   [2013-9-6 下午10:12:54]   
	 * @Version:      [v1.0] 
	 */
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		/*Notification notification = new Notification.Builder(this)
									.setContentText("my message")
									.setSmallIcon(R.drawable.img1)
									.setContentTitle("new mail")
									.build();		
		*/
		Notification notification = new Notification(R.drawable.img1 , "my message" ,System.currentTimeMillis());
		
		
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		Intent notificationIntent = new Intent(StatusBarActivity.this , firstActivity.class);
		PendingIntent pedingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		notification.setLatestEventInfo(this , "have a message" , "hhhhhhh" , pedingIntent);
		
		notificationManager.notify(R.drawable.img2 , notification);
	}
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值