posts - 93,  comments - 2,  trackbacks - 0

1、PendingIntent作用

根据字面意思就知道是延迟的intent,主要用来在某个事件完成后执行特定的Action。PendingIntent包含了Intent及Context,所以就算Intent所属程序结束,PendingIntent依然有效,可以在其他程序中使用。
常用在通知栏及短信发送系统中。

PendingIntent一般作为参数传给某个实例,在该实例完成某个操作后自动执行PendingIntent上的Action,也可以通过PendingIntent的send函数手动执行,并可以在send函数中设置OnFinished表示send成功后执行的动作。


2.举例(通知栏应用)
    界面A 定义一个notification    
    NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.notification;
    long when = System.currentTimeMillis()+2000;
    Notification n = new Notification(icon,"标题",when);
    n.defaults = Notification.DEFAULT_SOUND;
    n.flags |= Notification.FLAG_AUTO_CANCEL;
    Intent intent = new Intent(this,B.class);
    PendingIntent pi = new PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
    n.setLatesEventInfo(this,"通知栏demo提醒title","通知栏demo提醒text",pi);
    nm.notify(0,n);

    B.界面Intent intent = getIntent();
  String title = intent.getStringExtra("title");

    效果,当A界面显示,生成一个按钮,点击该按钮生成如上所示的通知栏,点击通知栏,则显示B界面,参数title为所显示的值。

3、Intent和PendingIntent的区别

a. Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel
b. Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效
c. PendingIntent自带Context,而Intent需要在某个Context内运行
d. Intent在原task中运行,PendingIntent在新的task中运行


posted on 2013-05-22 15:34 Terry Zou 阅读(234) 评论(0)  编辑  收藏 所属分类: Android

只有注册用户登录后才能发表评论。


网站导航:
 
<2013年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜