【问题标题】:How PendingIntent triggers intent?PendingIntent 如何触发意图?
【发布时间】:2019-04-08 08:54:19
【问题描述】:

例如,我们有。

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

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

不明白怎么触发?

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

感谢您的帮助。

附言 我想有一个简单的方法可以在意图明确时检查 Class 对象

    Intent intent = new Intent(context, clazz):

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

但是对于隐含的意图,我无法意识到发生了什么。

【问题讨论】:

  • 让我改写一下,实际上。我确定这不是很清楚。你到底在问什么?您是在问最终的Intent 发送者如何知道它是哪个PendingIntent 类型,因此要使用哪个start*()/send*() 方法?
  • @MikeM。是的,我问的正是这个
  • 好吧,当你调用一个特定的方法时,返回的PendingIntent 是为那个特定的组件类型配置的。也就是说,它在内部设置了一个标志,在触发Intent 之前对其进行检查。例如,拨打getActivity()the flag is set to ActivityManager.INTENT_SENDER_ACTIVITY时; getBroadcast()it's ActivityManager.INTENT_SENDER_BROADCAST
  • 非常感谢

标签: android android-intent android-pendingintent


【解决方案1】:

当您收到PendingIntent 时,您可以拨打getActivity()getBroadcast()getService()。创建PendingIntent 时,Intent 的类型包含在PendingIntent 中,以便它知道Intent 是用于ActivityBroadcastReceiver 还是Service

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

注意:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多