andoid中pendingIntent和Intent的区别

2024-06-18 07:08

本文主要是介绍andoid中pendingIntent和Intent的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前AppContext,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。常和alermanger notificationmanager一起使用。
Intent一般是用作ActivitySercviceBroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intentPendingIntent是对Intent一个包装。

Java代码 

private void showNotify(){   

        Notification notice=new Notification();   

        notice.icon=R.drawable.icon;   

        notice.tickerText="您有一条新的信息";   

        notice.defaults=Notification.DEFAULT_SOUND;   

        notice.when=10L;   

        // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒     

        //notice.vibrate = new long[] { 100, 250, 100, 500 };出错?   

        //notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0));   

10 notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转   

11         NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);   

12         manager.notify(0,notice);   

13     }  

[java]view plaincopyprint?

14 private void showNotify(){  

15         Notification notice=new Notification();  

16         notice.icon=R.drawable.icon;  

17         notice.tickerText="您有一条新的信息";  

18         notice.defaults=Notification.DEFAULT_SOUND;  

19         notice.when=10L;  

20         // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒    

21         //notice.vibrate = new long[] { 100, 250, 100, 500 };出错?  

22         //notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0));  

23 notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转  

24         NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);  

25         manager.notify(0,notice);  

26     }  

private void showNotify(){

        Notification notice=new Notification();

        notice.icon=R.drawable.icon;

        notice.tickerText="您有一条新的信息";

        notice.defaults=Notification.DEFAULT_SOUND;

        notice.when=10L;

        // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒  

        //notice.vibrate = new long[] { 100, 250, 100, 500 };出错?

        //notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0));

notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, new Intent(this,Activity2.class), 0));//即将跳转页面,还没跳转

        NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);

        manager.notify(0,notice);

    }


1. GSM网络中android发送短信示例

Java代码 

27 String msg ="你好,美女";   

28 String number = "135****6784";   

29 SmsManager sms = SmsManager.getDefault();   

30   

31 PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);   

32 sms.sendTextMessage(number, null, msg, pi, null);   

33 Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();  

[java]view plaincopyprint?

34 String msg ="你好,美女";  

35 String number = "135****6784";  

36 SmsManager sms = SmsManager.getDefault();  

37   

38 PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);  

39 sms.sendTextMessage(number, null, msg, pi, null);  

40 Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();  

String msg ="你好,美女";

String number = "135****6784";

SmsManager sms = SmsManager.getDefault();

 

PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);

sms.sendTextMessage(number, null, msg, pi, null);

Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();

 


代码解释
      PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去 。
      函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
      1PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
      2PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的PendingIntent广播。
      查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。

 

PendingIntent就是一个可以在满足一定条件下执行的Intent,它相比于Intent的优势在于自己携带有Context对象,这样他就不必依赖于某个activity才可以存在。

 

这篇关于andoid中pendingIntent和Intent的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1071619

相关文章

在Dockerfile中copy和add的区别及说明

《在Dockerfile中copy和add的区别及说明》COPY和ADD都是Dockerfile中用于文件复制的命令,但COPY仅用于本地文件或目录的复制,不支持自动解压缩;而ADD除了复制本地文件或... 目录在dockerfile中,copy 和 add有什么区别?COPY 命令ADD 命令总结在Doc

解读Pandas和Polars的区别及说明

《解读Pandas和Polars的区别及说明》Pandas和Polars是Python中用于数据处理的两个库,Pandas适用于中小规模数据的快速原型开发和复杂数据操作,而Polars则专注于高效数据... 目录Pandas vs Polars 对比表使用场景对比Pandas 的使用场景Polars 的使用

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

java中不同版本JSONObject区别小结

《java中不同版本JSONObject区别小结》本文主要介绍了java中不同版本JSONObject区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1. FastjsON2. Jackson3. Gson4. org.json6. 总结在Jav

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

2.1/5.1和7.1声道系统有什么区别? 音频声道的专业知识科普

《2.1/5.1和7.1声道系统有什么区别?音频声道的专业知识科普》当设置环绕声系统时,会遇到2.1、5.1、7.1、7.1.2、9.1等数字,当一遍又一遍地看到它们时,可能想知道它们是什... 想要把智能电视自带的音响升级成专业级的家庭影院系统吗?那么你将面临一个重要的选择——使用 2.1、5.1 还是

Python中@classmethod和@staticmethod的区别

《Python中@classmethod和@staticmethod的区别》本文主要介绍了Python中@classmethod和@staticmethod的区别,文中通过示例代码介绍的非常详细,对大... 目录1.@classmethod2.@staticmethod3.例子1.@classmethod

Golan中 new() 、 make() 和简短声明符的区别和使用

《Golan中new()、make()和简短声明符的区别和使用》Go语言中的new()、make()和简短声明符的区别和使用,new()用于分配内存并返回指针,make()用于初始化切片、映射... 详细介绍golang的new() 、 make() 和简短声明符的区别和使用。文章目录 `new()`

Python中json文件和jsonl文件的区别小结

《Python中json文件和jsonl文件的区别小结》本文主要介绍了JSON和JSONL两种文件格式的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下... 众所周知,jsON 文件是使用php JSON(JavaScripythonpt Object No