我有一個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需要等待很長時間時纔會出現錯誤,但如果飛濺工作中斷(更多時間),爲什麼在取消(更少時間)時它會失敗?
謝謝
覆蓋Activity的onStop方法,並把splashDelay.cancel()在那。所以只要你的splashDelay也一旦你的活動停止。 – rajpara
添加onStop,同樣的事情仍然發生。 – NewUserSOF