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


当前回答

顾名思义…PendingIntent

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

其他回答

其他应用程序可以使用的未来意图。 这里有一个创建一个的例子:

Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, 0);

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

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

In an easy language, 1. A description of an Intent and Target action to perform. First you have to create an intent and then you have to pass an specific java class which you want to execute, to the Intent. 2. You can call those java class which is your class action class by PendingIntent.getActivity, PendingIntent.getActivities(Context, int, Intent[], int), PendingIntent.getBroadcast(Context, int, Intent, int), and PendingIntent.getService(Context, int, Intent, int); Here you see that Intent which is comes from the step 1 3. You should keep in mind that...By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified.

这是我读了很长时间后学到的。

在我的例子中,上述答案和谷歌的官方文档都没有帮助我抓住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基本上是一个包装另一个Intent对象的对象。然后它可以传递给一个外部应用程序,在那里你授予该应用程序执行操作的权利,即,执行意图,就像从你自己的应用程序的进程执行一样(相同的权限和身份)。出于安全原因,您应该始终将显式意图传递给PendingIntent,而不是隐式意图。

 PendingIntent aPendingIntent = PendingIntent.getService(Context, 0, aIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);