Notification 学习

本文介绍如何使用Android的NotificationManager和Notification组件实现应用程序的通知功能。文章详细解释了创建通知的过程,包括实例化NotificationManager,设置Notification属性,并通过NotificationManager发出通知。此外,还介绍了PendingIntent的作用及其与Intent的区别。
package demo.notification;

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 android.view.View;
import android.widget.Button;

/**
 * 
 * 由于BroadcastReceiver并没有提供可视化的信息提示
 * 要想实现可视化的信息提示,比如,显示广播信息的内容,图标,以及振动等信息
 * 可以使用NotificationManager和Notification来实现
 * 一、获取系统级的服务NotificationManager,通过调用getSystemService(NOTIFICATION_SERVICE)来实现
 * 二、然后实例化Notification,设置其属性
 * 三、通过NotificationManager发出通知
 */
public class MainActivity extends Activity {
	
	private Button btnSend, btnCancel;
	
	private Notification n;
	
	private NotificationManager nm;
	
	private static final int NOTIFICATION_ID = 1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.btnSend = (Button)this.findViewById(R.id.send);
        this.btnCancel = (Button)this.findViewById(R.id.cancel);
        
        this.btnSend.setOnClickListener(sendListener);
        this.btnCancel.setOnClickListener(cancelListener);
        
        //实例化NotificationManager
        nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
        
        n = new Notification();
        
        //设置Notification的属性
        n.icon = R.drawable.icon; //显示的图标
        n.tickerText = "Notification Demo"; //显示的内容
        n.when = System.currentTimeMillis(); //显示时间
        
    }
    
    private View.OnClickListener sendListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			//发送通知
			Intent intent = new Intent(MainActivity.this, MainActivity.class); //首先实例化一个Intent
			//看到这里,想必大家会问PendingIntent是干什么的,Intent和PendingIntent是什么关系,呵呵,往下看:
			//其实,PendingIntent就是Intent的描述。可能会问,为什么要对这个Intent进行描述呢?
			//因为,我想把我的Intent交给另一个应用程序,让他在稍后的时间去完成我在Intent中安排的任务
			//那么直接使用Intent不行吗?是的,不能直接使用Intent,因为Intent没有这个权利。Intent只属于他所在的
			//应用程序,不能传递给别的应用程序。
			//所以,这里PendingIntent的用处就清晰了。呵呵。而且,PendingIntent的生命周期,由系统管理,创建他的应用
			//被Kill了,该PendingIntent可以照样存在,在别的进程中照样可以使用。Intent就没有这样的特性了。
			PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); //获取PendingIntent
			//设置事件信息
			n.setLatestEventInfo(MainActivity.this, "The title", "The content", pIntent);
			//发出通知
			nm.notify(NOTIFICATION_ID, n); //这里指定了一个ID,取消的时候,根据这个ID号
		}
	};
	
	private View.OnClickListener cancelListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			// 取消通知
			nm.cancel(NOTIFICATION_ID);
		}
	};
}
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
    <Button
    	android:id="@+id/send" 
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content" 
    	android:text="发出通知" />
    	
    <Button
    	android:id="@+id/cancel" 
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content" 
    	android:text="取消通知" />    	
</LinearLayout>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值