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

http://blog.csdn.net/illusion_luna/article/details/8122259


今天通过在网上学习了很多关于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的内容

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:gravity="center"  
  11.         android:textColor="#EEE"  
  12.         android:textStyle="bold"  
  13.         android:textSize="25sp"  
  14.         android:text="Notification应用的小案例"/>  
  15.     <Button   
  16.         android:id="@+id/btnSend"  
  17.         android:text="send notification"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"/>  
  20.   
  21. </LinearLayout>  
里面就一个Button和一个TextView。

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

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:gravity="center"  
  11.         android:textColor="#EEE"  
  12.         android:textStyle="bold"  
  13.         android:textSize="25sp"  
  14.         android:text="显示通知界面"/>  
  15.     <Button   
  16.         android:id="@+id/btnCancel"  
  17.         android:text="cancel notification"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"/>  
  20. </LinearLayout>  

一样的简单。。。。

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

[java]  view plain  copy
 print ?
  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7.   
  8. public class MainActivity extends Activity {  
  9.     /** Called when the activity is first created. */  
  10.     private Button btnSend=null;  
  11.     private static final String NotificationDemo_Action = "com.ceo.notification.activity.NotificationDemo_Action";  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         btnSend=(Button) findViewById(R.id.btnSend);  
  17.         btnSend.setOnClickListener(new OnClickListener() {  
  18.               
  19.             @Override  
  20.             public void onClick(View v) {  
  21.                 // TODO Auto-generated method stub  
  22.                 Intent intent=new Intent();  
  23.                 intent.setAction(NotificationDemo_Action);  
  24.                 sendBroadcast(intent);  
  25.             }  
  26.         });  
  27.     }  
  28. }  

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

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

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

[java]  view plain  copy
 print ?
  1. public class SecondActivity extends Activity {  
  2.   
  3.     private Button btnCancel=null;  
  4.     private Notification notification=null;  
  5.     //private Notification Secondnotifi=null;  
  6.     private NotificationManager nm=null;  
  7.     private static int ID=1;  
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         // TODO Auto-generated method stub  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.second);  
  13.         btnCancel = (Button)findViewById(R.id.btnCancel);   
  14.         //实例化一个NotificationManager管理对象  
  15.         nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  16.           
  17.         //实例化一个Notification对象  
  18.         notification=new Notification();  
  19.         //设置Notification的图标属性  
  20.         notification.icon=android.R.drawable.star_on;  
  21.         String tickerText="Test Notification";  
  22.         long when=System.currentTimeMillis();  
  23.         //设置Notification的标题属性  
  24.         notification.tickerText=tickerText;  
  25.         //设置Notification被通知的时间  
  26.         notification.when=when;  
  27.           
  28.         //创建一个Intent对象,用于实现跳转到指定的Activity  
  29.         Intent intent=new Intent(this,MainActivity.class);  
  30.         /*PendingIntent这个类顾名思义:Pending有延迟的意思,也就是说PendingIntent就是将一个Intent(意图) 
  31.          * 包裹起来延迟执行。 
  32.          * 通过PendingIntent的静态方法getActivity来获取PendingIntent的实例,其中除了 
  33.          * getActivity还有getBroadcast、getService,具体用什么,你可以根据上面Intent的功能决定 
  34.          * 你到底是想跳转到另一个activity呢?还是开启服务或者是发送广播等等。 
  35.          * 参数解释: 
  36.          * 1)context--当前的context对象 
  37.          * 2)想PendingIntent发送私有的请求数值,默认为0 
  38.          * 3)你要包括的Intent对象 
  39.          * 4)这个我也不太清楚。。。。。*/  
  40.         PendingIntent pi=PendingIntent.getActivity(this0, intent, 0);  
  41.         /*设置这个通知要显示的具体内容,其中的pi就是我们上面实例化的PendingIntent对象,就是说,当 
  42.          * 我们查看这个通知,并点击这个通知后,程序将会执行被PendingIntent包裹起来的Intent的指定动作, 
  43.          * 这里是跳转到MainActivity。。。。*/  
  44.         notification.setLatestEventInfo(this"消息""Hello Yang", pi);  
  45.           
  46.         /*Secondnotifi=new Notification(); 
  47.         Secondnotifi.icon=android.R.drawable.star_off; 
  48.         Secondnotifi.tickerText="Second Text"; 
  49.         Secondnotifi.when=System.currentTimeMillis(); 
  50.          
  51.         Intent second=new Intent(this,MainActivity.class); 
  52.         PendingIntent Spi=PendingIntent.getActivity(this, 0,second,0); 
  53.         Secondnotifi.setLatestEventInfo(this, "我来了", "Hello Tian", Spi); 
  54.          
  55.         nm.notify(2, Secondnotifi);*/  
  56.         /*再通过NotificationManager的notify()发放发布通知。注意,这里要传入一个ID,这个ID将作为 
  57.         *被NotificationManager管理的Notification的唯一标记。 
  58.         */  
  59.         nm.notify(ID, notification);  
  60.           
  61.           
  62.         btnCancel.setOnClickListener(new OnClickListener() {  
  63.               
  64.             @Override  
  65.             public void onClick(View v) {  
  66.                 // TODO Auto-generated method stub  
  67.                 //根据Notification的ID取消通知的发送  
  68.                 nm.cancel(ID);  
  69.             }  
  70.         });  
  71.     }  
  72. }  

下面再来看一下MyReeceiver这个类

[java]  view plain  copy
 print ?
  1. public class MyReeceiver extends BroadcastReceiver{  
  2.   
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         // TODO Auto-generated method stub  
  6. <span style="white-space:pre">  </span>//实例化Intent    
  7.         Intent i = new Intent();    
  8.         //在新任务中启动Activity    
  9.         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
  10.         //设置Intent启动的组件名称    
  11.         i.setClass(context, SecondActivity.class);    
  12.         //启动Activity,显示通知    
  13.         context.startActivity(i);  
  14.     }  
  15.   
  16. }  
点击运行这个android项目

当我点击"send notification"按钮

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

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

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值