我阅读了Android文档,但我仍然需要更多的说明。PendingIntent到底是什么?
当前回答
我在通知中遇到了PendingIntents。这里有一个简单的解释:
我们想要提供一个Intent到Notification,在这种情况下,我们想要打开一个执行相机捕捉功能的Activity。这里,如果我们简单地传递Intent, NotificationManager没有这个权限,尽管我的应用程序在Manifest中声明了这个权限;因此,该动作不会工作,因为NotificationManager没有这样做的权限。
但是,如果你使用PendingIntent,这里的权限,我的应用程序将被使用而不是NotificationManager。因此,即使NotificationManager没有相机权限,而我的应用程序有,它仍然会打开活动并执行应用程序。
注意:Pending intent需要常规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
在我的例子中,上述答案和谷歌的官方文档都没有帮助我抓住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(...)
}
}
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
你可以过一段时间再做。这是另一个意图。它是一种将你的任务交给其他应用程序来执行的方式。
悬而未决的意图
暂挂意图是对常规意图的包装 供另一个应用程序使用。 它使其他应用程序能够执行包含的功能 行动,因为它是你的应用程序与所有的权限 申请已获批准
当你想打开一些应用程序组件,如活动/服务/BroadcastReceiver在以后的时间或指定的时间间隔后,你必须发送PendingIntent在这种情况下。它就像你给其他应用程序一段时间后代表你运行你的应用程序代码的许可条。因此,PendingIntent可以超越进程边界,比如你想要AlarmManager,这是另一个进程中的另一个应用程序,然后AlarmManager在你的应用程序上执行由PendingIntent指定的操作
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。