【发布时间】:2014-02-21 21:09:48
【问题描述】:
我有一个使用启动屏幕和选择屏幕的应用程序,如下面的清单所示:
<application
android:name="com.example.CoolApp"
android:label="@string/app_name"
android:icon="@drawable/app_icon_debug"
android:theme="@style/Theme.NoBackground">
<activity
android:name="com.example.Splash"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:noHistory="true"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="coolappscheme"/>
</intent-filter>
</activity>
<activity
android:name="com.example.ChoiceActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden"/>
</application>
闪屏显示大约 2 秒,然后通过以下代码导航到 ChoiceActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
... some stuff for showing the Splash screen ...
Thread mythread = new Thread() {
@Override
public void run() {
try {
.. animation stuff for fading in ...
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {
} finally {
Intent intent = new Intent(Splash.this, ChoiceActivity.class);
startActivity(intent);
}
}
};
现在,很明显这段代码有很多问题(首先是为什么作者决定使用单独的Thread 而不是AsyncTask。但是,暂时先把这些东西放在一边,如果用户执行以下操作:
- 启动应用程序并查看启动画面。
- 强制应用程序进入后台(例如,用户接到电话,或者可能只是点击了“主页”按钮)。
然后,当Thread 完成时(即到达finally 块并创建Intent),创建新的ChoiceActivity,但它也将应用程序带到前台。我想禁用此行为。也就是说,我希望加载活动,但应用程序保留在后台。如果这不可能,那么我希望应用程序延迟加载新的Activity(//any// 新的Activity),直到应用程序恢复之后。
我怎样才能做到这一点?
【问题讨论】:
标签: java android multithreading android-activity background-process