我正在做一個應用程序的SplashScreen ...當應用程序啓動時,它開始LoadingActivity ...睡3秒,完成();然後啓動MainActivity。 Splash用於更新數據庫。如果數據庫已經更新,無論如何我仍然想要飛濺3秒。在開始另一個活動之前使用Thread.sleep(毫秒)等待一點,這是不好的做法嗎?
我使用下面的代碼:
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(LoadingActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
它是一個不好的初步實踐?爲什麼?該應用程序在AVD中運行得非常好。
如果您在主用戶界面中運行睡眠,那麼您將凍結用戶界面,並且會導致活動不響應錯誤(ANR)。使用處理程序postdelayed函數。 – Tarun
在主線程上執行阻塞操作*總是*一個壞主意,句號。 'postdelayed'就是答案。 –