Android Thread在运行设备Nexus 5时关闭

时间:2014-04-27 13:35:09

标签: android multithreading nexus-5

我有两个活动:Main和Splash。我的目标是显示Splash活动5秒,然后启动Main活动。当我在计算机的模拟器上启动Splash活动时,一切正常。当正在运行的设备是我的手机Nexus 5(插入电脑)时,手机会显示一点点(2 - 3秒)的Splash活动,然后转入主屏幕,然后(2 - 3秒后)开始主要活动。当我用apk文件在手机上安装应用程序时,一切正常。我的主课没有什么特别的。如果重要的话,我正在使用Android Studio。我的启动画面只是普通的图像。我的Splash活动:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            Thread timer = new Thread() {
                public void run() {
                    try {
                        sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        startActivity(new Intent("com.example.app.MAIN"));
                    }
                }
            };
            timer.start();
        }

        @Override
        protected void onPause() {
            super.onPause();
            finish();
        }

logcat的:

04-27 16:59:34.655    2502-2502/? I/Crashlytics﹕ Initializing Crashlytics 1.1.10.12
04-27 16:59:34.735    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 316K, 3% free 16924K/17272K, paused 12ms+5ms, total 34ms
04-27 16:59:34.735    2502-2523/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 12ms
04-27 16:59:34.735    2502-2516/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 12ms
04-27 16:59:34.745    2502-2502/? E/OpenUDID﹕ Initialisation isn't done
04-27 16:59:34.775    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 444K, 3% free 16992K/17468K, paused 2ms+2ms, total 20ms
04-27 16:59:34.775    2502-2515/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 4ms
04-27 16:59:34.785    2502-2502/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 5ms
04-27 16:59:34.785    2502-2530/? D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 5ms
04-27 16:59:34.785      761-809/? I/ActivityManager﹕ Killing 2332:com.android.providers.calendar/u0a1 (adj 15): empty #17
04-27 16:59:34.835    2502-2502/? V/AmazonInsightsSDK﹕ Firing Session Event: _session.start
04-27 16:59:34.835    2502-2502/? I/AmazonInsightsSDK﹕ Amazon Insights SDK(2.1.16.0) initialization successfully completed
04-27 16:59:34.845    2502-2536/? I/AmazonInsightsSDK﹕ Attempting to retrieve variation(s) for project(s):'Sponsored in feed'
04-27 16:59:34.865    2502-2506/? D/dalvikvm﹕ GC_CONCURRENT freed 462K, 3% free 17024K/17516K, paused 3ms+3ms, total 30ms

1 个答案:

答案 0 :(得分:0)

您需要从UI线程调用startActivity。一种简单的方法是使用AsyncTask而不是直接创建自己的线程。

    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            startActivity(new Intent("com.example.app.MAIN"));
        }

    }.execute();