什么是 Android PendingIntent?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2808796/
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
What is an Android PendingIntent?
提问by Rakesh
I am a newbie to Android. I read the Android Documentation but I still need some more clarification. Can anyone tell me what exactly a PendingIntent
is?
我是安卓的新手。我阅读了 Android 文档,但我仍然需要更多说明。谁能告诉我a到底PendingIntent
是什么?
回答by Lie Ryan
A PendingIntent
is a token that you give to a foreign application (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.
APendingIntent
是您提供给外部应用程序(例如NotificationManager
,AlarmManager
主屏幕AppWidgetManager
或其他第 3 方应用程序)的令牌,它允许外部应用程序使用您的应用程序的权限来执行一段预定义的代码。
If you give the foreign application an Intent, it will execute your Intent
with its own permissions. But if you give the foreign application a PendingIntent
, that application will execute your Intent
using your application's permission.
如果你给外部应用一个 Intent,它会Intent
用它自己的权限执行你的。但是,如果您给外部应用程序一个PendingIntent
,该应用程序将Intent
使用您的应用程序的权限执行您的操作。
回答by Samuh
A Pending Intent is a token you give to some app to perform an action on your apps' behalf irrespectiveof whether your application process is alive or not.
挂起的意图是象征你给一些应用程序来执行代表您的应用程序的操作,不论你的应用程序是活着还是没有。
I think the documentation is sufficiently detailed: Pending Intent docs.
我认为文档足够详细: Pending Intent docs。
Just think of use-cases for Pending Intents like (Broadcasting Intents, scheduling alarms) and the documentation will become clearer and meaningful.
想想 Pending Intents 的用例,比如(广播 Intents,调度警报),文档就会变得更加清晰和有意义。
回答by ???
In my case, none of above answers nor google's official documentationhelped me to grab the concept of PendingIntent
class.
就我而言,上述答案和谷歌的官方文档都没有帮助我理解PendingIntent
类的概念。
And then I found this video, Google I/O 2013, Beyond the Blue Dotsession. In this video, ex-googler Jaikumar Ganesh explains what PendingIntent
is, and that was the thing gave me the big picture of this.
然后我找到了这个视频,Google I/O 2013,Beyond the Blue Dotsession。在这段视频中,前谷歌员工 Jaikumar Ganesh 解释了这PendingIntent
是什么,这就是给我一个大局观的东西。
Below is just transcription of above video (from 15:24).
以下只是上述视频的转录(来自 15:24)。
So what's a pending intent?
It's a token that your app process will give to the location process, and the location process will use it to wake up your app when an event of interest happens. So this basically means that your app in the background doesn't have to be always running. When something of interest happens, we will wake you up. This saves a lot of battery.
那么什么是待定意图?
它是您的应用程序进程将提供给位置进程的令牌,当感兴趣的事件发生时,位置进程将使用它来唤醒您的应用程序。所以这基本上意味着您的应用程序在后台不必总是运行。当感兴趣的事情发生时,我们会叫醒你。这样可以节省很多电池。
This explanation becomes more clear with this snippet of code(which is included in the session's slide).
通过这段代码(包含在会话的幻灯片中),这个解释变得更加清晰。
PendingIntent mIntent = PendingIntent.getService(...);
mLocationClient.requestLocationUpdates(locationRequest, mIntent);
public void onHandleIntent(Intent intent) {
String action = intent.getAction();
if (ACTION_LOCATION.equals(action)) {
Location location = intent.getParcelableExtra(...)
}
}
回答by Kiran
Why PendingIntent is required ?I was thinking like
为什么需要 PendingIntent ?我在想
- Why the receiving application itself cannot create the
Intent
or - Why we cannot use a simple
Intent
for the same purpose.
- 为什么接收应用程序本身不能创建
Intent
或 - 为什么我们不能
Intent
为了同样的目的使用简单的。
E.g.Intent bluetoothIntent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
例如Intent bluetoothIntent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
If I send bluetoothIntent
to another application, which doesn't have permission android.permission.BLUETOOTH_ADMIN
, that receiving application cannot enable Bluetooth with startActivity(bluetoothIntent)
.
如果我发送bluetoothIntent
到另一个没有权限的android.permission.BLUETOOTH_ADMIN
应用程序,则该接收应用程序无法使用startActivity(bluetoothIntent)
.
The limitation is overcome using PendingIntent
. With PendingIntent
the receiving application, needn't have android.permission.BLUETOOTH_ADMIN
for enabling Bluetooth. Source.
使用PendingIntent
. 与PendingIntent
接收应用程序,不必android.permission.BLUETOOTH_ADMIN
启用蓝牙。来源。
回答by Arun P
Pending intent is an intent which will start at some point in the future. Normal intent starts immediately when it is passed to startActivity(Intent)
or StartService(Intent)
.
待定意图是将在未来某个时间点开始的意图。正常意图在传递给startActivity(Intent)
or时立即启动StartService(Intent)
。
回答by Oded Breiner
A future intent that other apps can use.
And here's an example for creating one:
其他应用程序可以使用的未来意图。
这是创建一个的示例:
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, 0);
回答by ARK
TAXI ANALOGY
出租车类比
Intent
意图
Intents are typically used for starting Services. For example:
意图通常用于启动服务。例如:
Intent intent = new Intent(CurrentClass.this, ServiceClass.class);
startService(intent);
This is like when you call for a taxi:
这就像你叫出租车时:
Myself = CurrentClass
Taxi Driver = ServiceClass
Pending Intent
待定意向
You will need to use something like this:
你将需要使用这样的东西:
Intent intent = new Intent(CurrentClass.this, ServiceClass.class);
PendingIntent pi = PendingIntent.getService(parameter, parameter, intent, parameter);
getDataFromThirdParty(parameter, parameter, pi, parameter);
Now this Third party will start the service acting on your behalf. A real life analogy is Uber or Lyft who are both taxi companies.
现在,该第三方将代表您启动服务。现实生活中的类比是 Uber 或 Lyft,它们都是出租车公司。
You send a request for a ride to Uber/Lyft. They will then go ahead and call one of their drivers on your behalf.
您向 Uber/Lyft 发送叫车请求。然后他们将继续代表您致电他们的一名司机。
Therefore:
所以:
Uber/Lyft ------ ThirdParty which receives PendingIntent
Myself --------- Class calling PendingIntent
Taxi Driver ---- ServiceClass
回答by srinu
A PendingIntent is a token that you give to another application (e.g. Notification Manager, Alarm Manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code. To perform a broadcast via a pending intent so get a PendingIntent via PendingIntent.getBroadcast(). To perform an activity via an pending intent you receive the activity via PendingIntent.getActivity().
PendingIntent 是您提供给另一个应用程序(例如通知管理器、警报管理器或其他第 3 方应用程序)的令牌,它允许其他应用程序使用您的应用程序的权限来执行预定义的代码段。要通过挂起的意图执行广播,请通过 PendingIntent.getBroadcast() 获取 PendingIntent。要通过挂起的意图执行活动,您可以通过 PendingIntent.getActivity() 接收活动。
回答by Narendra Motwani
What is an Intent?
什么是意图?
An Intent is a specific command in Android that allows you to send a command to the Android OS to do something specific. Think of it like an action that needs to take place. There are many actions that can be done such as sending an email, or attaching a photo to an email or even launching an application. The logical workflow of creating an intent is usually as follows: a. Create the Intent b. Add Intent options -> Ex. what type of intent we are sending to the OS or any attributes associated with that intent, such as a text string or something being passed along with the intent c. RUN the Intent
Intent 是 Android 中的特定命令,它允许您向 Android 操作系统发送命令以执行特定操作。把它想象成一个需要发生的动作。可以执行许多操作,例如发送电子邮件、将照片附加到电子邮件甚至启动应用程序。创建意图的逻辑工作流程通常如下:创建意图 B. 添加意图选项 -> 例如。我们向操作系统发送什么类型的意图或与该意图相关的任何属性,例如文本字符串或与意图一起传递的内容 c. 运行意图
Real Life Example: Let's say I wake up in the morning and I "INTEND" to go to the washroom. I will first have to THINK about going to the washroom, but that DOESN'T really get me to the washroom. I will then have to tell my brain to get out of bed first, then walk to the washroom, and then release, then go and wash my hands, then go and wipe my hands. Once I know where I'm going I SEND the command to begin and my body takes action.
现实生活示例:假设我早上醒来,我“打算”去洗手间。我首先要考虑去洗手间,但这并不能真正让我去洗手间。然后我必须告诉我的大脑先下床,然后走到洗手间,然后释放,然后去洗手,然后去擦手。一旦我知道我要去哪里,我就会发送命令开始,我的身体就会采取行动。
What is Pending Intents?
什么是待处理的意图?
Continuing from the real life example, let's say I want to take a shower but I want to shower AFTER I brush my teeth and eat breakfast. So I know I won't be showering until at least 30-40 minutes. I still have in my head that I need to prepare my clothes, and then walk up the stairs back to the bathroom, then undress and then shower. However this will not happen until 30-40 minutes have passed. I now have a PENDING intent to shower. It is PENDING for 30-40 minutes.
继续现实生活中的例子,假设我想洗澡,但我想在刷牙和吃早餐后洗澡。所以我知道我至少要到 30-40 分钟才能洗澡。我脑子里还想着我需要准备好衣服,然后走上楼梯回到浴室,然后脱掉衣服,然后淋浴。然而,这要等到 30-40 分钟过去后才会发生。我现在有一个待处理的淋浴意图。等待 30-40 分钟。
That is pretty much the difference between a Pending Intent and a Regular Intent. Regular Intents can be created without a Pending Intent, however in order to create a Pending Intent you need to have a Regular Intent setup first.
这几乎是 Pending Intent 和常规 Intent 之间的区别。可以在没有 Pending Intent 的情况下创建常规 Intent,但是为了创建 Pending Intent,您需要先设置常规 Intent。
回答by Gowtham Subramaniam
PendingIntent
is basically an object that wraps another Intent
object. Then it can be passed to a foreign application where you're granting that app the right to perform the operation, i.e., execute the intent as if it were executed from your own app's process (same permission and identity). For security reasons you should always pass explicit intents to a PendingIntent rather than being implicit.
PendingIntent
基本上是一个包装另一个Intent
对象的对象。然后它可以被传递到一个外部应用程序,在那里你授予该应用程序执行操作的权利,即执行意图,就像它是从你自己的应用程序的进程中执行的一样(相同的权限和身份)。出于安全原因,您应该始终将显式意图传递给 PendingIntent 而不是隐式的。
PendingIntent aPendingIntent = PendingIntent.getService(Context, 0, aIntent,
PendingIntent.FLAG_CANCEL_CURRENT);