Android的Notification应用详解和PendingIntent的初步剖解

今天通过在网上学习了很多关于android Notification的应用,但网上很多的资料都是说的模棱两可,模糊不清、有些也不说的很清晰,只直接上代码,也不加注释,看的蛋疼至极,所以经过我一个下午的努力收集资料,自己总结了Notification相关的知识,所以赶快写编博客,跟大家分享~~。

先来说一下要用android notification的相关步骤:

1)实例化一个NotificationManager实例--该实例的作用就是管理Notification实例,例如将其显示、排列、删除等等。

2)实例化一个Notification实例--该实例你可以这样理解,因为android是java编程的,而java是一种面向对象的语言,这个我想大家都清楚吧,那好了,那Notification你可以将它理解为一个对象,这个对象包括图标、显示文本、标题。。。。而且这个对象被NotificationManager实例管理。

3)初始化Notification的属性,包括上面所提到的图标、显示文本、标题。。。。。。

4)实例化一个Intent对象--我们都知道Intent在android中的翻译是"意图"的意思,顾名思义,这个对象也指明了我们的意图,到底是跳转到一个activity呢?还是开启一个服务或者接受一个广播等等。

5)实例化一个PendingIntent对象--这个对象的作用下面作说明,现在你只要知道它是一个可以包括Intent对象(就是上一步我们实例化的那个Intent)的一个特殊的Intent类。

6)通过Notification的实例.setLatestEventInfo(context, contentTitle, contentText, contentIntent)将上面实例化的PendingIntent装载进Notification对象里面。

7)通过NotificationManager实例.notify();将这个Notification进行管理,包括显示、删除等等。

好了,,接下来上代码,在代码中在逐步讲解。先来看一下整个项目的结构


我们先来看一下main.xml的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#EEE"
        android:textStyle="bold"
        android:textSize="25sp"
        android:text="Notification应用的小案例"/>
    <Button 
        android:id="@+id/btnSend"
        android:text="send notification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
里面就一个Button和一个TextView。

再来看一下second.xml这个xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
	<TextView 
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:gravity="center"
	    android:textColor="#EEE"
	    android:textStyle="bold"
	    android:textSize="25sp"
	    android:text="显示通知界面"/>
	<Button 
	    android:id="@+id/btnCancel"
	    android:text="cancel notification"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"/>
</LinearLayout>

一样的简单。。。。

再来看一下MainActivity这个Activity类的代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    private Button btnSend=null;
    private static final String NotificationDemo_Action = "com.ceo.notification.activity.NotificationDemo_Action";
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnSend=(Button) findViewById(R.id.btnSend);
        btnSend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setAction(NotificationDemo_Action);
				sendBroadcast(intent);
			}
		});
    }
}

这里面的代码也很简单,有个button,注册了点击的事件监听,当点击事件发生通过Intent啦发送一个广播。

private static final String NotificationDemo_Action = "com.ceo.notification.activity.NotificationDemo_Action";
其中这句声明了广播接收过滤的标准。至于这点不懂的,大家可以去看一下关于BroadcastReceiver的官方API,由于这里不是说BroadcastReceiver,所以在这里就不赘述了,原谅。

再来看一下SecondActivity这个Activity类的代码

public class SecondActivity extends Activity {

	private Button btnCancel=null;
	private Notification notification=null;
	//private Notification Secondnotifi=null;
	private NotificationManager nm=null;
	private static int ID=1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		btnCancel = (Button)findViewById(R.id.btnCancel); 
		//实例化一个NotificationManager管理对象
		nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		//实例化一个Notification对象
		notification=new Notification();
		//设置Notification的图标属性
		notification.icon=android.R.drawable.star_on;
		String tickerText="Test Notification";
		long when=System.currentTimeMillis();
		//设置Notification的标题属性
		notification.tickerText=tickerText;
		//设置Notification被通知的时间
		notification.when=when;
		
		//创建一个Intent对象,用于实现跳转到指定的Activity
		Intent intent=new Intent(this,MainActivity.class);
		/*PendingIntent这个类顾名思义:Pending有延迟的意思,也就是说PendingIntent就是将一个Intent(意图)
		 * 包裹起来延迟执行。
		 * 通过PendingIntent的静态方法getActivity来获取PendingIntent的实例,其中除了
		 * getActivity还有getBroadcast、getService,具体用什么,你可以根据上面Intent的功能决定
		 * 你到底是想跳转到另一个activity呢?还是开启服务或者是发送广播等等。
		 * 参数解释:
		 * 1)context--当前的context对象
		 * 2)想PendingIntent发送私有的请求数值,默认为0
		 * 3)你要包括的Intent对象
		 * 4)这个我也不太清楚。。。。。*/
		PendingIntent pi=PendingIntent.getActivity(this, 0, intent, 0);
		/*设置这个通知要显示的具体内容,其中的pi就是我们上面实例化的PendingIntent对象,就是说,当
		 * 我们查看这个通知,并点击这个通知后,程序将会执行被PendingIntent包裹起来的Intent的指定动作,
		 * 这里是跳转到MainActivity。。。。*/
		notification.setLatestEventInfo(this, "消息", "Hello Yang", pi);
		
		/*Secondnotifi=new Notification();
		Secondnotifi.icon=android.R.drawable.star_off;
		Secondnotifi.tickerText="Second Text";
		Secondnotifi.when=System.currentTimeMillis();
		
		Intent second=new Intent(this,MainActivity.class);
		PendingIntent Spi=PendingIntent.getActivity(this, 0,second,0);
		Secondnotifi.setLatestEventInfo(this, "我来了", "Hello Tian", Spi);
		
		nm.notify(2, Secondnotifi);*/
		/*再通过NotificationManager的notify()发放发布通知。注意,这里要传入一个ID,这个ID将作为
		*被NotificationManager管理的Notification的唯一标记。
		*/
		nm.notify(ID, notification);
		
		
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//根据Notification的ID取消通知的发送
				nm.cancel(ID);
			}
		});
	}
}

下面再来看一下MyReeceiver这个类

public class MyReeceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
	//实例化Intent  
        Intent i = new Intent();  
        //在新任务中启动Activity  
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        //设置Intent启动的组件名称  
        i.setClass(context, SecondActivity.class);  
        //启动Activity,显示通知  
        context.startActivity(i);
	}

}
点击运行这个android项目

当我点击"send notification"按钮

注意频幕的上部,就会出现一条通知,当我点着那条通知往下拉

就会显示出这条通知的具体内容,大家可以对照一下,当我点击这条消息后

就会启动Intent的内设置好的意图。
好了 这些代码全部写完了,至于还有些Notification的属性,大家可以去参考一下官方的API,那里写的很详细,另外,大家也可以去学一下要是有两条通知该怎么办?

在这里我就不说明白了,希望大家自己动手吧~~~呵呵呵呵。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值