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


当前回答

悬而未决的意图

暂挂意图是对常规意图的包装 供另一个应用程序使用。 它使其他应用程序能够执行包含的功能 行动,因为它是你的应用程序与所有的权限 申请已获批准

当你想打开一些应用程序组件,如活动/服务/BroadcastReceiver在以后的时间或指定的时间间隔后,你必须发送PendingIntent在这种情况下。它就像你给其他应用程序一段时间后代表你运行你的应用程序代码的许可条。因此,PendingIntent可以超越进程边界,比如你想要AlarmManager,这是另一个进程中的另一个应用程序,然后AlarmManager在你的应用程序上执行由PendingIntent指定的操作

其他回答

一个PendingIntent是一个令牌,你给另一个应用程序(例如通知管理器,警报管理器或其他第三方应用程序),它允许这个其他应用程序使用你的应用程序的权限来执行预定义的代码段。 要通过挂起意图执行广播,请通过PendingIntent. getbroadcast()获取一个PendingIntent。要通过挂起intent来执行一个activity,你需要通过PendingIntent.getActivity()来接收这个activity。

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

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

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

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

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

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

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

出租车的类比

意图

intent通常用于启动Services。例如:

Intent intent = new Intent(CurrentClass.this, ServiceClass.class);
startService(intent);

这就像你叫出租车的时候:

Myself = CurrentClass
Taxi Driver = ServiceClass

悬而未决的意图

你需要使用像这样的东西:

Intent intent = new Intent(CurrentClass.this, ServiceClass.class);
PendingIntent pi = PendingIntent.getService(parameter, parameter, intent, parameter);
getDataFromThirdParty(parameter, parameter, pi, parameter);

现在这个第三方将代表您启动服务。 现实生活中的一个类比是Uber或Lyft,它们都是出租车公司。

你向优步/来福车发出打车请求。然后他们会代表你打电话给他们的司机。

因此:

Uber/Lyft ------ ThirdParty which receives PendingIntent
Myself --------- Class calling PendingIntent
Taxi Driver ---- ServiceClass