Android Intent 和 PendingIntent 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24257247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Differences between Intent and PendingIntent
提问by user3629316
I read through some articles and both seem to do the same thing and I was wondering what is the difference between starting the service like that:
我阅读了一些文章,两者似乎都在做同样的事情,我想知道像这样启动服务有什么区别:
Intent intent = new Intent(this, HelloService.class);
startService(intent);
or like that:
或者像这样:
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, MyService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
As I read through, these two do the same thing, if in the service you return a parameter START_STICKY;
当我通读时,这两个做同样的事情,如果在服务中你返回一个参数 START_STICKY;
回答by Siddharth_Vyas
Intent
意图
An Android Intent is an object carrying an intent, i.e. a message from one component to another component either inside or outside of the application. Intents can communicate messages among any of the three core components of an application -- Activities, Services, and BroadcastReceivers.
Android Intent 是一个携带意图的对象,即从一个组件到应用程序内部或外部的另一个组件的消息。意图可以在应用程序的三个核心组件(活动、服务和广播接收器)中的任何一个组件之间传递消息。
The intent itself, an Intent object, is a passive data structure. It holds an abstract description of an operation to be performed.
Intent 本身是一个 Intent 对象,是一种被动的数据结构。它包含要执行的操作的抽象描述。
For example: say you have an Activity that needs to launch an email client and send an email. To do this, your Activity would send an Intent with the action ACTION_SEND
, along with the appropriate chooser, to the Android Intent Resolver:
例如:假设您有一个 Activity 需要启动电子邮件客户端并发送电子邮件。为此,您的 Activity 会向ACTION_SEND
Android Intent Resolver发送带有 action 的 Intent以及适当的选择器:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
The specified chooser gives the proper interface for the user to pick how to send your email data.
指定的选择器为用户提供了适当的界面来选择如何发送您的电子邮件数据。
EXPLICIT INTENTS
明确的意图
// Explicit Intent by specifying its class name
Intent i = new Intent(this, TargetActivity.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
// Starts TargetActivity
startActivity(i);
IMPLICIT INTENTS
隐含意图
// Implicit Intent by specifying a URI
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.example.com"));
// Starts Implicit Activity
startActivity(i);
Pending Intent
待定意向
A PendingIntent is a token that you give to a foreignapplication (e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.
PendingIntent 是您提供给外部应用程序(例如 NotificationManager、AlarmManager、Home Screen AppWidgetManager 或其他 3rd 方应用程序)的令牌,它允许外部应用程序使用您的应用程序的权限来执行一段预定义的代码。
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 (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: almost always, for example, the base Intent you supply should have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.
通过向另一个应用程序提供 PendingIntent,您授予它执行您指定的操作的权利,就好像另一个应用程序是您自己一样(具有相同的权限和身份)。因此,你应该小心你如何构建 PendingIntent:几乎总是,例如,你提供的基本 Intent 应该将组件名称显式设置为你自己的组件之一,以确保它最终被发送到那里而不是其他地方。
Example for Pending Intent : http://android-pending-intent.blogspot.in/
待定意图示例:http: //android-pending-intent.blogspot.in/
Source : Android Intentsand Android Pending Intents
来源:Android Intents和Android Pending Intents
Hope this helps.
希望这可以帮助。
回答by HungNM2
PendingIntent
is a wrapper of Intent
. The foreign app that receives the PendingIntent
, doesn't know the content of Intent
which is wrapped by PendingIntent
. The mission of foreign app is to send back the intent to owner when some conditions are met (For example: alarm with schedule, or notification with click...). The conditions are given by owner but processed by foreign app (For example: alarm, notification).
PendingIntent
是Intent
. 接收外国的应用程序PendingIntent
,不知道的内容Intent
是由包裹PendingIntent
。国外应用的任务是在满足某些条件时将意图发送回所有者(例如:带有时间表的警报,或带有点击的通知......)。条件由所有者给出,但由外部应用程序处理(例如:警报、通知)。
If foreign app sent intent to your app, mean that foreign app know about the content of the intent. and foreign app make decision to send intent then your app must process intent to meet some conditions => your app get performance resource of system.
如果外国应用程序向您的应用程序发送了意图,则意味着外国应用程序知道意图的内容。并且外国应用程序决定发送意图,那么您的应用程序必须处理意图以满足某些条件 => 您的应用程序获取系统的性能资源。
回答by Zumry Mohamed
Another simple difference:
另一个简单的区别:
Normal intent will die as soon as the app is killed.
Pending intents never die. They will be alive as long as it's needed by alarm service, location service, or any other services.
正常意图会在应用程序被杀死后立即消失。
挂起的意图永远不会消亡。只要警报服务、定位服务或任何其他服务需要它们,它们就会一直存在。
回答by My God
Starting services regularly via AlarmManager
通过 AlarmManager 定期启动服务
As with activities the Android system may terminate the process of a service at any time to save resources. For this reason you cannot simply use a TimerTask
in the service to ensure that it is executed on a regular basis.
与活动一样,Android 系统可以随时终止服务进程以节省资源。因此,您不能简单地TimerTask
在服务中使用 a来确保它定期执行。
So, for correct scheduling of the Service use the AlarmManager
class.
因此,为了正确安排服务,请使用AlarmManager
该类。
UPDATE:
更新:
So there is no actual difference between the two. But depending on whether you want to ensure the execution of the service or not, you can decide what to use as for the formerthere is no guarantee and for the laterit is.
因此,两者之间没有实际区别。但是根据您是否要确保服务的执行,您可以决定使用什么,前者没有保证,后者则可以。
More info at AndroidServices.
更多信息在AndroidServices。
回答by kupsef
Functionally, there is no difference.
在功能上,没有区别。
The meaning of PendingIntent is that, you can handle it to other application that later can use it as if the other application was yourself. Here is the relevant explanation from the documentation:
PendingIntent 的意思是,你可以把它处理给其他应用程序,以后可以像其他应用程序是你自己一样使用它。以下是文档中的相关解释:
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 (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: almost always, for example, the base Intent you supply should have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it.
通过向另一个应用程序提供 PendingIntent,您授予它执行您指定的操作的权利,就好像另一个应用程序是您自己一样(具有相同的权限和身份)。因此,你应该小心你如何构建 PendingIntent:几乎总是,例如,你提供的基本 Intent 应该将组件名称显式设置为你自己的组件之一,以确保它最终被发送到那里而不是其他地方。
PendingIntent 本身只是对系统维护的令牌的引用,该令牌描述用于检索它的原始数据。
So PendingIntent is just a reference to the data that represents the original Intent (that used to create the PendingIntent).
因此 PendingIntent 只是对表示原始 Intent(用于创建 PendingIntent)的数据的引用。