android - PendingIntent 如何触发 Intent ?

标签 android android-intent android-pendingintent

例如,我们有。

PendingIntent pi= PendingIntent.getActivity(context, requestCode, intent, FLAG);

当我们将 pi 对象传递给其他类或其他应用程序时,我们知道包装在 pi 中的 Intent 稍后会被该类或应用程序触发,对吗?

不明白会怎么触发?

当我们使用 intent 手动/立即启动组件时,我们调用对应于确切组件类型的方法(startActivity(intent)、startService(intent) 等),据我所知有没有像 startAnyComponent(intent).

这样的方法

感谢您的帮助。

附言 我想有一种简单的方法可以通过在 Intent 明确时检查 Class 对象

    Intent intent = new Intent(context, clazz):

在这种情况下,系统可以检查 clazz.isAssignableFrom(Service.class) 并调用 startService(intent)。

但对于隐式 Intent ,我无法意识到发生了什么。

最佳答案

当您获得 PendingIntent 时,您可以调用 getActivity()getBroadcast()getService()。当创建 PendingIntent 时,Intent 的类型包含在 PendingIntent 中,以便它知道 Intent用于 ActivityBroadcastReceiverService

稍后,当使用 PendingIntent 时,会调用从 PendingIntent 获取 IntentSender。要实际发送 Intent,将调用 IntentSender.sendIntent()IntentSender 知道 PendingIntent 是如何创建的,因此知道它是否应该使用 startActivity()sendBroadcast()startService() 以正确发送 Intent

注意:

关于您关于如何使用显式 Intent 工作的假设:这是行不通的,因为通常实际发送 Intent 的应用程序没有访问您应用的代码,因此无法加载您的类,也无法使用 Class.isAssignableFrom() 确定 Intent 的类型。

关于android - PendingIntent 如何触发 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55569587/

相关文章:

来自待处理 Intent 的 Android bundle 始终接收为 null

Android 导航 Controller 错误 `no current navigation code`

Eclipse 中的 Android NDK 调试 - 如何停止仅在步入 native 代码时发生的段错误/SIGILL

java - 如何在谷歌地图 v2 android 上显示纬度/经度标记

android - 在按钮上单击切换 xml 布局页面

java - 使用 PendingIntent 停止服务

android - 如何在屏幕上向上滑动手指时增大和减小音乐播放器的音量?

android - android 4.0.3 上操作栏中的 "back button"似乎使我的应用程序崩溃

java - getIntent() 返回空指针异常

android - AlarmManager 不适用于时间选择器