1

我有一个两部分的初始屏幕,我一直在尝试在 Android 2.2(eclipse 模拟器)上工作,一切都在 2.3 模拟器中工作。在 2.2 中,它的工作频率更高——有时会黑屏——有时我需要重新启动模拟器。如果我尝试单击屏幕以取消启动并直接进入主要活动,它将进入黑屏并在日志中看到:

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 必须等待太长时间时出现“启动超时已过期”错误,但是如果启动工作中断(更多时间),为什么在取消(更少时间)时它会失败?

谢谢

4

1 回答 1

0

异步任务不太适合在初始屏幕上设置计时器。更合适的方法是使用Handler和 sendDelayed 来延迟启动画面。

于 2012-06-30T17:24:29.320 回答