我阅读了Android文档,但我仍然需要更多的说明。PendingIntent到底是什么?
当前回答
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是一个令牌,你给另一个应用程序(例如通知管理器,警报管理器或其他第三方应用程序),它允许这个其他应用程序使用你的应用程序的权限来执行预定义的代码段。 要通过挂起意图执行广播,请通过PendingIntent. getbroadcast()获取一个PendingIntent。要通过挂起intent来执行一个activity,你需要通过PendingIntent.getActivity()来接收这个activity。
简单来说
悬而未决的意图基本上是一种意图,你可以传递给其他应用程序或服务,如通知管理器,警报管理器等,让他们处理何时是正确的时间/行为执行。
出租车的类比
意图
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
未决意图是一个令牌,你给一些应用程序执行你的应用程序的代表,不管你的应用程序进程是否活跃。
我认为文件是足够详细的: 未决的意图文档。
只要想想Pending Intents的用例(broadcast Intents, scheduling alarms),文档就会变得更加清晰和有意义。
我在通知中遇到了PendingIntents。这里有一个简单的解释:
我们想要提供一个Intent到Notification,在这种情况下,我们想要打开一个执行相机捕捉功能的Activity。这里,如果我们简单地传递Intent, NotificationManager没有这个权限,尽管我的应用程序在Manifest中声明了这个权限;因此,该动作不会工作,因为NotificationManager没有这样做的权限。
但是,如果你使用PendingIntent,这里的权限,我的应用程序将被使用而不是NotificationManager。因此,即使NotificationManager没有相机权限,而我的应用程序有,它仍然会打开活动并执行应用程序。
注意:Pending intent需要常规intent先被设置。
推荐文章
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?
- createScaledBitmap的过滤器参数做什么?
- 为什么我在使用adb时访问数据文件夹被拒绝?