Android Studio startActivity

Android Studio startActivity,android,Android,我自己在找,但没找到。我可以在后台启动像谷歌翻译这样的应用程序吗?下面列出的程序运行翻译非常好,但同时,我的应用程序进入后台。我想在后台启动翻译器时,让我的应用程序处于前台。非常感谢 Intent app_to_launch = getPackageManager(). getLaunchIntentForPackage("com.google.android.apps.translate"); if (app_to_launch != null) { start

我自己在找,但没找到。我可以在后台启动像谷歌翻译这样的应用程序吗?下面列出的程序运行翻译非常好,但同时,我的应用程序进入后台。我想在后台启动翻译器时,让我的应用程序处于前台。非常感谢

Intent app_to_launch = getPackageManager().
            getLaunchIntentForPackage("com.google.android.apps.translate");
if (app_to_launch != null) {
    startActivity(app_to_launch);
}
更新:

跟随主题。观察到一件事。下面列出的代码工作正常:

            Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.google.android.apps.translate");
            if (app_to_launch != null) {
                startActivity(app_to_launch);
            }

            try{Thread.sleep(1000);}catch (Exception e){};

            Intent intent = new Intent(MainActivity.this, WordsStatus.class);
            startActivity(intent);
但是如果我取消1秒暂停(Thread.sleep),就没有迹象表明谷歌翻译程序已经运行了。似乎第二次启动(我的应用程序启动)会抑制第一次启动(Google translator启动)。如果我恢复1秒暂停,一切正常。

确保只需执行以下操作:

Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.package.of.background.app");
            if (app_to_launch != null) {
                startActivity(app_to_launch);
            }
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.package.of.forground.app");
            if (app_to_launch != null) {
                startActivity(app_to_launch);
            }
(第二次应用程序启动不需要使用软件包管理器,您可以使用任何启动活动的方法,即如果您想启动自己应用程序的内部活动,则只需使用正常方法
startActivity(new Intent(this,MyActivity.class))

这将启动两个应用程序,第二个启动将移动堆栈中的第一个1秒

请注意,应用程序并非真正“在后台运行”。任何未在屏幕上显示的应用程序通常处于暂停状态(但可能已启动后台服务以执行某些工作)