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


当前回答

为什么需要PendingIntent ?我在想

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

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

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

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

其他回答

未决意图是一个令牌,你给一些应用程序执行你的应用程序的代表,不管你的应用程序进程是否活跃。

我认为文件是足够详细的: 未决的意图文档。

只要想想Pending Intents的用例(broadcast Intents, scheduling alarms),文档就会变得更加清晰和有意义。

待定意图是指将在未来某个时间点启动的意图。正常的意图在传递给startActivity(intent)或StartService(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用于开启蓝牙。源。

未决意图是指向其他应用程序提供执行特定工作的所有许可的意图。当主活动被销毁时,Android操作系统会收回主活动的权限。