我阅读了Android文档,但我仍然需要更多的说明。PendingIntent到底是什么?
当前回答
未决意图是一个令牌,你给一些应用程序执行你的应用程序的代表,不管你的应用程序进程是否活跃。
我认为文件是足够详细的: 未决的意图文档。
只要想想Pending Intents的用例(broadcast Intents, scheduling alarms),文档就会变得更加清晰和有意义。
其他回答
待定意图是指将在未来某个时间点启动的意图。正常的意图在传递给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(...)
}
}
出租车的类比
意图
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
我在通知中遇到了PendingIntents。这里有一个简单的解释:
我们想要提供一个Intent到Notification,在这种情况下,我们想要打开一个执行相机捕捉功能的Activity。这里,如果我们简单地传递Intent, NotificationManager没有这个权限,尽管我的应用程序在Manifest中声明了这个权限;因此,该动作不会工作,因为NotificationManager没有这样做的权限。
但是,如果你使用PendingIntent,这里的权限,我的应用程序将被使用而不是NotificationManager。因此,即使NotificationManager没有相机权限,而我的应用程序有,它仍然会打开活动并执行应用程序。
注意:Pending intent需要常规intent先被设置。
顾名思义…PendingIntent
你可以过一段时间再做。这是另一个意图。它是一种将你的任务交给其他应用程序来执行的方式。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件