Android的:如何从BroadcastReceiver的恢复应用程序/活动?应用程序、Android、BroadcastReceiver

2023-09-12 05:35:42 作者:逆光夏花

如果我的步骤如下:

在启动活动A - > b活动 preSS主页按钮。 点击上的应用程序了。

结果:活动B'显示了(它的恢复)

Result: 'Activity B' shows up (it resumes).

在启动活动A - > b活动 preSS后退按钮。 点击上的应用程序了。

结果:活动A'显示出来(它的重新启动)

Result: 'Activity A' shows up (it restarts).

我想要做的的BroadcastReceiver完全相同。

I want to do exactly same from the BroadcastReceiver.

在启动活动A - > b活动 preSS主页按钮。 的BroadcastReceiver接收广播,并希望恢复应用程序。

我预期的结果:活动B'显示了

我想要做的的BroadcastReceiver完全相同。

I want to do exactly same from the BroadcastReceiver.

在启动活动A - > b活动 preSS后退按钮。 的BroadcastReceiver接收广播,并希望重启的应用程序。

当前结果:活动A显示了

随着code没有做什么,我希望:

Following code doesn't do what I expect:

public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, ActivityA.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

我也试过Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY,但没有运气。

I also tried "Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY" but no luck.

推荐答案

我的天哪,我做了它的工作!

My gosh, I made it working!!

感谢您为你们提供其他的答案,但他们不是我所期待的。

Thank you for other answers you guys provided, but they weren't what I was looking for.

这将做的工作:

Intent i = getPackageManager().getLaunchIntentForPackage("com.your.package.name");
i.setFlags(0);
i.setPackage(null);
startActivity(i);