Android 用户单击主页按钮后将应用程序置于最前面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12074980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bring application to front after user clicks on home button
提问by Zoombie
My application is in running mode[foreground]and user clicks on home button, which puts application to background[and still running]. I have alarm functionality in my application which fires up. I want is when my alarm goes off i want to bring my background running application in foreground and from last state in which it was.
我的应用程序处于运行模式 [foreground]并且用户单击主页按钮,这将应用程序置于后台 [并且仍在运行]。我的应用程序中有警报功能会启动。我想要的是当我的警报响起时,我想将我的后台运行应用程序置于前台并从它的最后状态开始。
<application
android:name="com.abc.android.state.management.MainEPGApp"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:largeHeap="true"
android:logo="@drawable/app_logo" >
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="nosensor"
android:theme="@style/Theme.Sherlock" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Starter"
android:configChanges="orientation|screenSize"
android:screenOrientation="behind"
android:launchMode="singleTop"
android:uiOptions="none"
android:windowSoftInputMode="adjustPan" />
</application>
回答by David Wasser
Intent intent = new Intent(context, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
You should use your starting or rootactivity for MyRootActivity
.
您应该将您的开始或根活动用于MyRootActivity
.
This will bring an existing task to the foreground without actually creating a new Activity. If your application is not running, it will create an instance of MyRootActivity
and start it.
这会将现有任务带到前台,而无需实际创建新活动。如果您的应用程序未运行,它将创建一个实例MyRootActivity
并启动它。
EDIT
编辑
I added Intent.FLAG_ACTIVITY_SINGLE_TOP
to Intent to make it really work!
我添加Intent.FLAG_ACTIVITY_SINGLE_TOP
到 Intent 使其真正起作用!
Second EDIT
第二次编辑
There is another way to do this. You can simulate the "launching" of the app the same way that Android launches the app when the user selects it from the list of available apps. If the user starts an application that is already running, Android just brings the existing task to the foreground (which is what you want). Do it like this:
还有另一种方法可以做到这一点。您可以模拟应用程序的“启动”,就像当用户从可用应用程序列表中选择应用程序时 Android 启动应用程序一样。如果用户启动一个已经在运行的应用程序,Android 只是将现有任务带到前台(这就是你想要的)。像这样做:
Intent intent = new Intent(context, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
// the activity from a service
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
回答by Smeet
You can use the below code to bring the application to front:
您可以使用以下代码将应用程序置于前台:
private void bringApplicationToFront()
{
Log.d(TAG, "====Bringging Application to Front====");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
try
{
pendingIntent.send();
}
catch (CanceledException e)
{
e.printStackTrace();
}
}
回答by Piotr Zawadzki
A combination that works for me is to use:
对我有用的组合是使用:
Intent bringToForegroundIntent = new Intent(context, RootActivity.class);
bringToForegroundIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(bringToForegroundIntent);
If you check the logs on the device whenever you start an activity from a launcher icon this is the intent that gets passed to launch the app or move it back to foreground if e.g. user clicked the Home button.
如果您在每次从启动器图标启动 Activity 时检查设备上的日志,这就是传递以启动应用程序或将其移回前台(例如用户单击主页按钮)的意图。
回答by jiaqiang huang
I find a new way to bring app to foreground, It imitate click Icon to launch application.
我找到了一种将应用程序带到前台的新方法,它模仿单击图标来启动应用程序。
PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(pkName);
if (intent != null)
{
//模拟点击桌面图标的启动参数
intent.setPackage(null);
// intent.setSourceBounds(new Rect(804,378, 1068, 657));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
context.startActivity(intent);
}
it work for me, But who can tell me why package must be null
它对我有用,但谁能告诉我为什么包必须为空
回答by Chaofan Zhang
From my test, to mimic the Launcher behavior, the key is to:
从我的测试来看,要模仿 Launcher 的行为,关键是:
intent.setPackage(null);
intent.setPackage(null);
after
后
Intent intent = packageManager.getLaunchIntentForPackage(pkName);
Intent intent = packageManager.getLaunchIntentForPackage(pkName);
Other methods in this thread doesn't work in my case.
这个线程中的其他方法在我的情况下不起作用。
Thanks to jiaqing's answer, I post this answer as I don't have the right to comment. I don't know the logic behind this either, I guess it's related with the task owner. Anyone knows, would be glad to know.
感谢嘉庆的回答,我无权发表评论,所以我发布了这个答案。我也不知道这背后的逻辑,我猜这与任务所有者有关。任何人都知道,会很高兴知道。
回答by Guykun
For an alarm, you want to take a look at starting an Android Service
对于闹钟,你想看看启动一个Android服务
This service will be more resilient than your application which may be killed while in the background and can fire off an intentto bring your application to the front (or restart it if it was killed) when it is time for the alarm to go off.
此服务将比您的应用程序更具弹性,后者可能会在后台被终止,并且可以在警报响起时触发将您的应用程序带到前台(或如果它被终止则重新启动它)的意图。
回答by sonal balekai
Using the ActivityManager class you can bring a running task to front
使用 ActivityManager 类,您可以将正在运行的任务放在前面
void bringToFront(){
ActivityManager activtyManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfos = activtyManager.getRunningTasks(3);
for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTaskInfos)
{
if (this.getPackageName().equals(runningTaskInfo.topActivity.getPackageName()))
{
activtyManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
return;
}
}
}