如何在 Api 级别 7 (Android 2.1) 中近似 PendingIntent getActivities?

发布于 2024-11-02 22:06:11 字数 655 浏览 1 评论 0原文

我希望有一个针对 Android 2.1、API 级别 7 的应用程序,当用户单击传入的 C2DM 通知时,立即启动多个活动。这是我当前用来启动活动的方法:

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)

此方法仅允许我将一项活动放入堆栈中。我真正想做的是使用这个方法:

public static PendingIntent getActivities (Context context, int requestCode, Intent[] intents, int flags)

这个方法报告说它仅适用于 API 级别 11,即 Android 3.0。我不想破坏与 2.1 的向后兼容性。谁能建议我如何在不依赖 Android 3.0 的情况下实现这种效果?我尝试寻找源< /a> 这个新方法,但它似乎还不可用。

I wish to have an app that targets Android 2.1, API level 7, launch multiple activities at once when a user clicks on a C2DM notification that has come in. This is the method I currently use to launch my activity:

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)

This method only allows me to put one activity on the stack. What I really want to do is use this method:

public static PendingIntent getActivities (Context context, int requestCode, Intent[] intents, int flags)

This method reports that it is only available for API level 11, which is Android 3.0. I do not wish to break backward compatibility with 2.1. Can anyone suggest how I might be able to achieve this effect without taking a dependency on Android 3.0? I tried looking for the source to this new method, but it does not appear to be available yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-11-09 22:06:11

您要做的就是有一个单独的活动作为警报的目标,并从那里构建意图堆栈,如下所示。这可能很容易推广到类似“getactivities”的东西 - 遗憾的是它不在兼容库中。

public class AlarmActivity extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

    intent = new Intent(this, ChildActivity.class);
    startActivity(intent);

    finish();
  }
}

What you do is have a separate activity that is the target of the alarm, and build the intent stack from there, as below. This could probably be generalised into something very like 'getactivities' quite easily - it's a pity it isn't in the compat libraries.

public class AlarmActivity extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

    intent = new Intent(this, ChildActivity.class);
    startActivity(intent);

    finish();
  }
}
半枫 2024-11-09 22:06:11

正如 MisterSquonk 在评论中所说,任何时候只能有一个 Activity 处于活动状态(即使在 3.0 中也是如此),因此“一次启动多个 Activity”是不可能的。即使是这样,当多个活动快速连续启动时,用户体验会是什么样的,并且不能保证哪个活动将最后启动,因此是留给用户交互的活动。

我怀疑您实际上希望同时唤醒应用程序的不同部分,而不需要每个部分都有自己的 UI。如果是这样,那么我建议拥有一个或多个针对通用意图过滤器实现多个广播接收器的服务。当您触发该事件的广播时,多个事物将同时被唤醒。

As MisterSquonk says in the comments, only one Activity can be active at any one time (even in 3.0), so launching "multiple activities at once" is not going to be possible. Even if it were, what will the user experience be like with multiple activities starting in quick succession, and no guarantee of which will be launched last, and so be the one left for the user to interact with.

I suspect that you actually want to wake up different parts of your app simultaneously without each one having its own UI. If so, then I would suggest having one or more Services which implement multiple BroadcastReceivers against a common Intent filter. When you fire a Broadcast of that event, then multiple things will get woken up at once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文