我的应用程序处于运行模式[前景],用户点击主页按钮,这将应用程序置于后台[并仍在运行].我的应用程序中有警报功能,它会启动.我想要的是当我的闹钟响起时,我希望将我的后台运行应用程序放在前台并从最后状态开始.
<application
android:name="nl.ziggo.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>
Run Code Online (Sandbox Code Playgroud)
Dav*_*ser 59
Intent intent = new Intent(context, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
您应该使用您的开始或根活动MyRootActivity
.
这将把现有任务带到前台而不实际创建新的Activity.如果您的应用程序未运行,它将创建一个实例MyRootActivity
并启动它.
编辑
我添加Intent.FLAG_ACTIVITY_SINGLE_TOP
到Intent以使其真正起作用!
第二次编辑
还有另一种方法可以做到这一点.当用户从可用应用列表中选择应用时,您可以像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);
Run Code Online (Sandbox Code Playgroud)
适合我的组合是使用:
Intent bringToForegroundIntent = new Intent(context, RootActivity.class);
bringToForegroundIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(bringToForegroundIntent);
Run Code Online (Sandbox Code Playgroud)
如果您从启动器图标启动活动时检查设备上的日志,则这是传递启动应用程序的意图,或者如果用户单击"主页"按钮,则将其移回前台.
您可以使用以下代码将应用程序置于最前面:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我找到了一种将应用程序带到前台的新方法,它模仿单击Icon启动应用程序.
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);
}
Run Code Online (Sandbox Code Playgroud)
它对我有用,但谁可以告诉我为什么包必须为空
小智 5
根据我的测试,要模仿启动器行为,关键是:
intent.setPackage(null);
后
Intent intent = packageManager.getLaunchIntentForPackage(pkName);
该线程中的其他方法不适用于我的情况。
感谢嘉庆的回答,我发布这个答案,因为我没有评论的权利。我也不知道这背后的逻辑,我猜这与任务所有者有关。任何人都知道,会很高兴知道。