1

我有一個2部分啓動畫面,我一直在試圖在Android 2.2(eclipse模擬器)上工作一切工作在2.3模擬器。在2.2版本中,它的工作頻率比較高 - 有時會進入黑屏 - 有時我需要重新啓動模擬器。如果我嘗試點擊屏幕取消飛濺,向右走的主要活動,它進入黑屏和在日誌中我看到:Android 2.2黑屏當AsyncTask在雙啓動畫面中取消

06-29 12:23:54.474: I/ActivityManager(58): Starting activity: Intent { cmp=org.test.game/.MainActivity } 
06-29 12:23:54.494: W/System.err(278): java.lang.InterruptedException 
06-29 12:23:54.504: D/qemud(37): fdhandler_accept_event: accepting on fd 10 
06-29 12:23:54.504: D/qemud(37): created client 0x17fe8 listening on fd 15 
06-29 12:23:54.504: D/qemud(37): client_fd_receive: attempting registration for service 'sensors' 
06-29 12:23:54.504: D/qemud(37): client_fd_receive: -> received channel id 11 
06-29 12:23:54.514: D/qemud(37): client_registration: registration succeeded for client 11 
06-29 12:23:54.524: D/qemud(37): fdhandler_accept_event: accepting on fd 10 
06-29 12:23:54.524: D/qemud(37): created client 0x18038 listening on fd 16 
06-29 12:23:54.524: D/qemud(37): fdhandler_event: disconnect on fd 15 
06-29 12:23:54.544: W/System.err(278): at java.lang.VMThread.sleep(Native Method) 
06-29 12:23:54.554: W/System.err(278): at java.lang.Thread.sleep(Thread.java:1306) 
06-29 12:23:54.554: W/System.err(278): at java.lang.Thread.sleep(Thread.java:1286) 
06-29 12:23:54.564: W/System.err(278): at org.test.game.Splash$SplashScreenDelay.doInBackground(Splash.java:121) 
06-29 12:23:54.574: W/System.err(278): at org.test.game.Splash$SplashScreenDelay.doInBackground(Splash.java:1) 
06-29 12:23:54.584: W/System.err(278): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
06-29 12:23:54.594: W/System.err(278): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
06-29 12:23:54.604: W/System.err(278): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
06-29 12:23:54.604: W/System.err(278): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
06-29 12:23:54.614: W/System.err(278): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
06-29 12:23:54.624: W/System.err(278): at java.lang.Thread.run(Thread.java:1096) 
06-29 12:24:04.475: W/ActivityManager(58): Launch timeout has expired, giving up wake lock! 
06-29 12:24:04.484: W/ActivityManager(58): Activity idle timeout for HistoryRecord{44eca1f8 org.test.game/.MainActivity} 
06-29 12:24:14.494: W/ActivityManager(58): Activity destroy timeout for HistoryRecord{44ebb9b0 org.test.game/.Splash} 

通過http://webgarbage.de/splash-screen-on-android.html

import android.app.Activity; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MotionEvent; 
import android.widget.ImageView; 

public class Splash extends Activity { 

    private AsyncTask splashDelay; 
    private ImageView ivSplash; 
    private int splashCount = 0; 
    private int splashTime = 2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splashscreen); 
     ivSplash = (ImageView) findViewById(R.id.IVSplash); 
     splashDelay = new SplashScreenDelay().execute(splashTime); 
    } 

    //Touch screen to skip splash 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      splashDelay.cancel(true); 
     } 
     return true; 
    } 

    private class SplashScreenDelay extends AsyncTask<Integer, Integer, Integer> { 

     @Override 
     protected Integer doInBackground(Integer... params) { 
      try { 
       int count = 0; 
       while (count < params[0]*10) { 
        if(count % 10 == 0) { 
         Log.v("Splash", "Sleeping... " + count/10); 
        } 
        Thread.sleep(100); 
        count++; 
       } 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      return new Integer(0); 
     } 

     @Override 
     protected void onPostExecute(Integer result) { 
      if (splashCount == 0) { 
       splashCount = 1; 
       ivSplash.setBackgroundResource(R.drawable.splash2); 
       splashDelay = new SplashScreenDelay().execute(splashTime); 
      } 
      else 
       startActivity(new Intent(getApplicationContext(), MainActivity.class)); 
     } 

     protected void onCancelled() { 
      startActivity(new Intent(getApplicationContext(), MainActivity.class)); 
     } 
    } 
} 

任何意見的啓發?什麼是取消飛濺給黑屏? 我讀過「啓動超時已過期」的錯誤,當MainActivity需要等待很長時間時纔會出現錯誤,但如果飛濺工作中斷(更多時間),爲什麼在取消(更少時間)時它會失敗?

謝謝

+0

覆蓋Activity的onStop方法,並把splashDelay.cancel()在那。所以只要你的splashDelay也一旦你的活動停止。 – rajpara

+0

添加onStop,同樣的事情仍然發生。 – NewUserSOF

回答

0

異步任務不適合在啓動畫面上設置計時器。更合適的方法是使用Handler和sendDelayed來延遲啓動屏幕。

+0

你能詳細說明一下嗎?這是一個普遍的事實還是僅僅是2.2?我讀過很多頁面,其中大部分都推薦Asynctask。 – NewUserSOF

+0

AsyncTask通常用於在單獨的線程上執行阻塞操作。上述任務沒有實際工作,並造成不穩定。處理程序和消息用於進程發信號。 sendMessageDelayed將完成與上述異步任務相同的過程,而不會過度複雜化該活動。 – purdyk

+0

爲了擴展這一點,Thread.sleep()通常是拖延你的應用程序的錯誤方式,除非它們非常小。更好地使用警報或延遲的消息。 – BenTobin