【问题标题】:In Android, how to start an activity of application B when user click on notification from application A?在 Android 中,当用户单击应用程序 A 的通知时,如何启动应用程序 B 的活动?
【发布时间】:2012-07-13 18:39:35
【问题描述】:

我在网上搜索了答案,但我得到的只是单击 A 的通知时如何在应用程序 A 中启动活动。问题是,要启动 B 的活动,我需要 B 的 Context 将其传递给 PendingIntent 以通知 A,但我无法在 A 中获取 B 的 Context。

有什么方法可以这样做,还是我应该做其他事情来启动应用 B 而不是使用 PendingIntent.getActivity() 方法?

【问题讨论】:

  • 两件事。 1) 在您发出待处理意图的位置发布代码。 2) 发布清单部分,描述应如何处理这些活动。
  • 那么有人会很快解决这个问题。
  • 另外,user1524355 也不是很有说服力的名字。
  • 好吧,我猜这个名字是为我自动生成的。我稍后会改变它:)

标签: android android-intent android-activity android-notifications android-context


【解决方案1】:

您不需要将 B 的上下文传递给待处理的 Intent,只需一个指向第二个活动的 Intent。

如果由于某种奇怪的原因这不起作用(尽管它应该),您可以将通知触发到 BraodcastReceiver 并在那里开始活动,但就像我说的那样,这不是必需的。

Intent secondActivity = new Intent(context, SecondActivity.class);

PendingIntent notificationIntent = PendingIntent.getActivity(
   context, 
   0, // or you can use some unique id
   secondActivity, 
   PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

  • 正确。除了您拥有的上下文之外,绝对不需要获取任何其他上下文。
  • 如何在没有上下文的情况下创建 PendingIntent 来启动活动? PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) 将上下文作为参数。
  • 是的,只需使用您当前拥有的 w/e 上下文、您的活动、w/e 应用程序。您需要一个上下文,而不是第二个活动的特定上下文。
  • @jug6ernaut 对于意图,我应该使用 Intent.makeMainActivity() 来创建它吗?
猜你喜欢
  • 2019-05-21
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2019-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多