目前我的應用程序從對話框和線程中的服務器獲取數據。 來電時應該做些什麼。 現在我正在這樣做。我想知道這種方法是對的還是別的什麼都要做。來電處理線程
@Override
protected void onPause() {
// TODO Auto-generated method stub
Utility.debugger("PAUSE 1");
if (ScreenReceiver.wasScreenOn) {
// this is the case when onPause() is called by the system due to a screen state change
Utility.killDialog();
System.out.println("SCREEN TURNED OFF");
} else {
Utility.debugger("PAUSE 2");
// this is when onPause() is called when the screen state has not changed
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
Utility.debugger("PAUSE 3");
if (!ScreenReceiver.wasScreenOn) {
// this is when onResume() is called due to a screen state change
System.out.println("SCREEN TURNED ON");
Utility.debugger("PAUSE 4");
Utility.resumeDialog();
} else {
// this is when onResume() is called when the screen state has not changed
}
super.onResume();
}
public static void killDialog()
{
if(dialog != null || dialog.isShowing())
{dialog.dismiss();
t.interrupt();
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void resumeDialog()
{
if(dialog != null )
{
dialog.setIndeterminate(true);
dialog.show();
t.run();
}
}
其中t是線程和對話框正在progressDialog。 我也喜歡暫停和恢復是設備進入睡眠狀態。
謝謝!
我想在傳入呼叫來臨時停止對話框和正在運行的線程並在呼叫結束時重新啓動,因爲它會返回應用程序。 – voidRy
@ user983364線程無法暫停。打斷他們通常是一個壞主意。但是,通常使用標誌來模擬這種行爲。例如'if(isPaused)Thread.sleep(100)' – spatulamania
thakx for information :)。 – voidRy