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


当前回答

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.

其他回答

待定意图是指将在未来某个时间点启动的意图。正常的意图在传递给startActivity(intent)或StartService(intent)时立即启动。

在我的例子中,上述答案和谷歌的官方文档都没有帮助我抓住PendingIntent类的概念。

然后我发现了这个视频,谷歌I/O 2013, Beyond Blue Dot会议。在这个视频中,前谷歌员工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(...)   
    }
}

顾名思义…PendingIntent

你可以过一段时间再做。这是另一个意图。它是一种将你的任务交给其他应用程序来执行的方式。

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

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.