我阅读了Android文档,但我仍然需要更多的说明。PendingIntent到底是什么?


当前回答

什么是意图?

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 as 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

现实生活中的例子:假设我早上醒来,我“打算”去洗手间。我首先要考虑去洗手间,但这并不能让我真正去洗手间。然后我必须告诉我的大脑先起床,然后去洗手间,然后放松,然后去洗手,然后去擦手。一旦我知道我要去哪里,我发送命令开始,我的身体采取行动。

什么是未决意图?

从现实生活中的例子继续,假设我想洗澡,但我想在刷牙和吃早餐后洗澡。所以我知道我至少要等30-40分钟才能洗澡。我脑子里还在想,我需要准备好衣服,然后上楼回到浴室,然后脱衣服,然后洗澡。然而,这要等到30-40分钟后才会发生。我现在有洗澡的意向。等待30-40分钟。

这就是挂起意图和常规意图之间的区别。常规意图可以在没有挂起意图的情况下创建,然而,为了创建挂起意图,你需要先设置一个常规意图。

其他回答

简单来说

悬而未决的意图基本上是一种意图,你可以传递给其他应用程序或服务,如通知管理器,警报管理器等,让他们处理何时是正确的时间/行为执行。

一个PendingIntent是一个令牌,你给一个外部应用程序(例如NotificationManager, AlarmManager, Home Screen AppWidgetManager,或其他第三方应用程序),它允许外部应用程序使用你的应用程序的权限来执行预定义的代码段。

如果你给了外部应用一个Intent,它会用自己的权限执行你的Intent。但是如果你给了外部应用一个PendingIntent,该应用将使用你的应用权限执行你的Intent。

Pending Intent指定将来要执行的操作。它允许你将未来的Intent传递给另一个应用程序,并允许该应用程序执行该Intent,就像它拥有与你的应用程序相同的权限一样,无论你的应用程序是否仍然存在,当Intent最终被调用时。

它是一个令牌,你给一个外部应用程序,它允许外部应用程序使用你的应用程序的权限来执行预定义的代码段。

如果你给了一个外部应用程序一个Intent,并且这个应用程序发送/广播你给的Intent,他们会用他们自己的权限执行这个Intent。但是如果你给外部应用一个你用自己的权限创建的Pending Intent,该应用将使用你的应用权限执行包含的Intent。

要通过挂起意图执行广播,请通过PendingIntent. getbroadcast()获取一个PendingIntent。要通过挂起intent来执行一个activity,你需要通过PendingIntent.getActivity()来接收这个activity。

这是一个你想要执行的Intent动作,但是在稍后的时间。就当是把一个意图冷冻起来。需要它的原因是必须从应用程序中的有效上下文中创建和启动Intent,但在某些情况下,当你想要运行操作时,Intent是不可用的,因为你在技术上处于应用程序的上下文之外(两个常见的例子是从Notification或BroadcastReceiver启动Activity。

通过创建一个PendingIntent你想要用它来启动,比如说,当你有上下文的时候(从另一个Activity或Service内部),你可以把这个对象传递给外部的东西,以便它代表你启动应用程序的一部分。

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.

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

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’s 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.

为什么需要PendingIntent ?我在想

为什么接收应用程序本身不能创建Intent或 为什么我们不能使用一个简单的意图达到同样的目的。

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

如果我将bluetoothIntent发送到另一个没有android。permission权限的应用程序。接收应用程序不能通过startActivity(bluetoothIntent)启用蓝牙。

使用PendingIntent可以克服这个限制。对于PendingIntent,接收应用程序不需要android.permission。BLUETOOTH_ADMIN用于开启蓝牙。源。

一个PendingIntent用一个令牌包装了一般的Intent,你给外部应用程序执行你的许可。 如:

你的音乐应用程序的通知不能播放/暂停音乐如果你 没有给PendingIntent发送广播,因为你的音乐应用 哪个通知应用程序具有READ_EXTERNAL_STORAGE权限 不喜欢。通知是一个系统服务(所以它是一个外部应用程序)。