什么是Android PendingIntent?

Rak*_*esh 510 android android-intent android-pendingintent

我是Android的新手.我阅读了Android文档,但我仍需要进一步澄清.谁能告诉我究竟PendingIntent是什么?

Lie*_*yan 861

A PendingIntent是您提供给外部应用程序(例如NotificationManager,AlarmManager主屏幕AppWidgetManager或其他第三方应用程序)的令牌,它允许外部应用程序使用您的应用程序的权限来执行预定义的代码段.

如果您为外部应用程序提供Intent,它将Intent使用自己的权限执行您的操作.但是,如果您提供外国申请PendingIntent,该申请将Intent使用您的申请许可执行您的申请.

  • @Tiago:在您的情况下,如果特权应用程序A为应用程序B提供待处理的意图,以便B可以在想要读取联系人数据时发送它.A的责任是询问用户用户想要向B提供哪些联系人数据,并且仅向B提供该数据.Pending Intent是一种权限提升机制,就像任何权限提升机制一样,具有强大的权力会带来很大的责任.用户有责任决定应用程序B是否对用户选择的联系人数据值得信任. (6认同)
  • 除了通知,你还在哪里看到过使用 pendingIntents ?我想我只看到通知使用它... (5认同)
  • @LieRyan 这个答案比 developer.google 关于pendingintent thx 的解释更好 (4认同)
  • @Johnny_D:它意味着它所说的,一般来说,你想要创建一个显式的Intent,其组件名称是一个绝对的名称,明确地引用你自己的一个类.否则,Intent可能会被发送到另一个应用程序,这可能会导致问题,因为Intent将在您的应用程序许可下运行. (4认同)
  • @LieRyan,这里的应用程序权限是我们在清单中指定的权限吗?例如.INTERNET许可? (3认同)
  • 难道这种"权限移交"的安全性真的很糟糕吗?我有一个应用程序A,我给了几个权限.然后我有应用程序B,它使用了很少的权限.如果应用程序A可以通过PendingIntent在应用程序B中启动某些东西(允许A的所有权限暂时在B上),那么B不能用这个权限在幕后做任何事情吗?例如,A可能有权读取用户的联系人但B却没有.如果A向B发送PendingIntent,则B可以读取联系人并执行恶意操作(例如将其发送到服务器). (2认同)

Sha*_*yaz 267

意图

Intent是Android中的标准消息传递机制,表示用户执行某些工作的意图.它们允许您与您或Android操作系统定义的其他组件进行交互.

例:

  1. 广播消息
  2. 启动相机
  3. 开始服务
  4. 发起一项活动
  5. 显示网页或联系人列表
  6. 拨打电话号码或接听电话

    它们以两种方式使用

1)由你来召唤一个组件

2)由系统通知您某些事件.

创建意图的逻辑工作流程通常如下:

  • 创建意图
  • 湾 添加意图选项 - > Ex.我们发送给操作系统的意图类型或与该意图相关的任何属性,例如文本字符串或与意图一起传递的内容
  • C.运行意图

真实生活示例:假设我早上醒来并且" 想要 "去洗手间.我首先要考虑去洗手间,但那并不能让我去洗手间.然后我必须告诉我的大脑先起床,然后走到洗手间,然后放开,然后去洗手,然后去擦我的手.一旦我知道我要去哪里,我就会发出命令开始,我的身体会采取行动.

的PendingIntent

A PendingIntent指定将来要采取的操作.它允许您将未来的Intent传递给另一个应用程序,并允许该应用程序执行该Intent,就像它具有的那样same permissions as your application,无论您的应用程序是否仍在最终调用Intent时.它是您提供给外部应用程序的令牌,允许外部应用程序使用您的应用程序的权限来执行预定义的代码段.

通过PendingIntent向另一个应用程序提供,您授予它执行您指定的操作的权利,就好像另一个应用程序是您自己(具有相同的权限和标识).因此,您应该注意如何构建 PendingIntent:例如,通常,您提供的基本Intent将组件名称显式设置为您自己的组件之一,以确保它最终发送到那里,而不是其他任何地方.

这是您想要执行的Intent操作,但稍后会执行.把它想象成冰上的意图吧.它需要的原因是因为必须从Context应用程序中的有效内容创建和启动Intent ,但是在某些情况下,当您想要运行该操作时,它不可用,因为您在技术上不在应用程序的上下文中(两个常见的例子是从通知启动一个活动,或者BroadcastReceiver创建一个PendingIntent你想用来启动一个活动的活动,你可以Context这样做(来自另一个活动或服务)

继续现实生活中的例子:假设我想洗澡,但我想在刷牙和吃早餐后洗澡.所以我知道我不会洗澡至少30-40分钟.我仍然想知道我需要准备好衣服,然后走上楼梯回浴室,然后脱衣服然后淋浴.但是,直到30-40分钟才会发生这种情况.现在我有一个PENDING意图淋浴.这是PENDING 30-40分钟.

这几乎是Pending Intent和Regular Intent之间的区别.简而言之:

常规意图 - > DOES NOT REQUIRE PENDING INTENT TO BE MADE

待定意图 - > REQUIRES A REGULAR INTENT TO BE CREATED

意图有两种类型 - 显式隐式

显式意图:当您的应用程序知道要调用哪个组件以执行某些操作时

隐含意图:当您的应用程序不知道哪个组件可以完全执行您想要的操作时.对于Ex,如果您只是想要显示URL,系统将决定哪个组件将实现该意图.

关于Intents的更好更明确的想法.Vist下面的链接

  1. Slidenerd.com
  2. Android Intent教程
  3. 多一点

  • @shakeeb Ayaz,为什么其他应用程序与我的应用程序具有相同的权限呢?当我们启动NotificationManager时,权限如何在此处显示?pendingIntent在这里扮演的角色是正常的意图吗? (5认同)

Sam*_*muh 48

待处理的意图是你给了一些应用程序令牌代表您的应用程序执行一个动作,不论是否你的应用程序是活着还是没有.

我认为文档足够详细: Pending Intent docs.

只需考虑Pending Intents的用例(广播意图,调度警报),文档就会变得更加清晰和有意义.


김준호*_*김준호 37

就我而言,上述答案和谷歌的官方文档都没有帮助我抓住PendingIntent课程的概念.

然后我发现了这个视频,Google I/O 2013,Beyond the Blue Dot会议.在这段视频中,前googler Jaikumar Ganesh解释了什么PendingIntent,这就是让我对此大局的事情.

以下是上述视频的转录(从15:24开始).

那么什么是悬而未决的意图?

这是您的应用程序流程将为位置流程提供的令牌,当感兴趣的事件发生时,位置流程将使用它来唤醒您的应用程序.所以这基本上意味着您的应用程序在后台不必始终运行.当感兴趣的事情发生时,我们会叫醒你.这节省了大量电池.

使用此代码片段(包含在会话的幻灯片中),此解释变得更加清晰.

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(...)   
    }
}
Run Code Online (Sandbox Code Playgroud)


Kir*_*ran 32

为什么需要PendingIntent?我在想

  1. 为什么接收应用程序本身无法创建Intent
  2. 为什么我们不能Intent为同一目的使用简单.

例如Intent bluetoothIntent= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

如果我发送bluetoothIntent到另一个没有权限的应用程序,则android.permission.BLUETOOTH_ADMIN接收应用程序无法启用蓝牙startActivity(bluetoothIntent).

使用可以克服限制PendingIntent.使用PendingIntent接收应用程序,无需android.permission.BLUETOOTH_ADMIN启用蓝牙.来源.

  • 您的bluetoothIntent示例非常有价值.谢谢 (3认同)
  • @Kiran - 如果接收应用程序可以打开蓝牙(使用 Pending Intent),那么为什么该应用程序没有在其清单中包含此权限?这就像我正在制作一个可以拨打电话的应用程序,但我没有包含 CALL_PHONE 权限,因为我希望其他应用程序向我发送一个挂起的意图来拨打这个电话。这是你想说的吗? (2认同)
  • @CopsOnRoad PendingIntent 的接收应用程序可以是一个通用应用程序,它接收任何请求并代表请求者执行请求。它的角色只是一个中间人,因此没有所有的权限。这种中间人应用程序的一个例子是通知管理器,它可以从通知启动任何想要的应用程序,而无需拥有许可。 (2认同)

小智 20

待定意图是一种意图,将在未来的某个时刻开始.正常意图在传递给startActivity(Intent)或时立即开始StartService(Intent).


Ode*_*ner 12

其他应用程序可以使用的未来意图.
以下是创建一个示例:

Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, 0);
Run Code Online (Sandbox Code Playgroud)

  • Downvote是因为你没有真正解释其他应用程序"未来"或可用的意图的特殊之处 (8认同)

Raj*_*Raj 12

TAXI模拟

意图

意图通常用于启动服务.例如:

Intent intent = new Intent(CurrentClass.this, ServiceClass.class);
startService(intent);
Run Code Online (Sandbox Code Playgroud)

这就像你打电话叫出租车一样:

Myself = CurrentClass
Taxi Driver = ServiceClass
Run Code Online (Sandbox Code Playgroud)

待定意图

你需要使用这样的东西:

Intent intent = new Intent(CurrentClass.this, ServiceClass.class);
PendingIntent pi = PendingIntent.getService(parameter, parameter, intent, parameter);
getDataFromThirdParty(parameter, parameter, pi, parameter);
Run Code Online (Sandbox Code Playgroud)

现在,这个第三方将开始代表您服务.真实的比喻是Uber或Lyft,他们都是出租车公司.

您向Uber/Lyft发送了乘车请求.然后他们将继续代表您致电其中一位司机.

因此:

Uber/Lyft ------ ThirdParty which receives PendingIntent
Myself --------- Class calling PendingIntent
Taxi Driver ---- ServiceClass
Run Code Online (Sandbox Code Playgroud)


小智 9

PendingIntent是您为另一个应用程序(例如,通知管理器,警报管理器或其他第三方应用程序)提供的令牌,它允许此其他应用程序使用您的应用程序的权限来执行预定义的代码段.要通过挂起的意图执行广播,请通过PendingIntent.getBroadcast()获取PendingIntent.要通过挂起的意图执行活动,您可以通过PendingIntent.getActivity()接收活动.


Nar*_*ani 6

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

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.

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.


Ash*_*wat 6

PendingIntent 使用您提供给外部应用程序的令牌包装一般 Intent,以便在您的许可下执行。例如:

如果您没有发送广播,您的音乐应用程序的通知将无法播放/暂停音乐,PendingIntent因为您的音乐应用程序具有READ_EXTERNAL_STORAGE通知应用程序没有的权限。通知是一项系统服务(因此它是一个外国应用程序)。


小智 5

待定意图指定将来要采取的操作。它允许您将未来的 Intent 传递给另一个应用程序,并允许该应用程序执行该 Intent,就好像它与您的应用程序具有相同的权限一样,无论最终调用 Intent 时您的应用程序是否仍然存在。

\n\n

它是您提供给外部应用程序的令牌,允许外部应用程序使用您的 application\xe2\x80\x99s 权限来执行预定义的代码段。

\n\n

如果您给外部应用程序一个 Intent,并且该应用程序发送/广播您提供的 Intent,它们将使用自己的权限执行该 Intent。但是,如果您向外部应用程序提供使用您自己的权限创建的待处理意图,则该应用程序将使用您的 application\xe2\x80\x99s 权限执行包含的意图。

\n\n

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().

\n\n

It is an Intent action that you want to perform, but at a later time. Think of it a putting an Intent on ice. The reason it\xe2\x80\x99s needed is because an Intent must be created and launched from a valid Context in your application, but there are certain cases where one is not available at the time you want to run the action because you are technically outside the application\xe2\x80\x99s context (the two common examples are launching an Activity from a Notification or a BroadcastReceiver.

\n\n

By creating a PendingIntent you want to use to launch, say, an Activity while you have the Context to do so (from inside another Activity or Service) you can pass that object around to something external in order for it to launch part of your application on your behalf.

\n\n

A PendingIntent provides a means for applications to work, even after their process exits. Its important to note that even after the application that created the PendingIntent has been killed, that Intent can still run. A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService (Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

\n\n

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: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

\n\n

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application\xe2\x80\x99s process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.

\n


Gow*_*iam 5

PendingIntent基本上是包装另一个Intent对象的对象。然后,可以将其传递给外部应用程序,在该外部应用程序中,您授予该应用程序执行操作的权限,即,执行该意图,就好像它是从您自己的应用程序的流程中执行一样(相同的权限和身份)。出于安全原因,您应该始终将显式意图传递给PendingIntent,而不是隐式。

 PendingIntent aPendingIntent = PendingIntent.getService(Context, 0, aIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)