Android Studio:应用运行的白色屏幕

时间:2016-05-25 12:49:55

标签: android android-studio google-cloud-messaging splash-screen

我正在开发一个应用程序,当我运行应用程序时,我会在启动屏幕之前获得白屏。

在启动画面上我在后台创建了数据库,我也有推送通知注册码。对于推送通知注册,请参阅this链接。所以我的启动画面代码代码如下:

public class SplashScreenActivity extends AppCompatActivity {
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private BroadcastReceiver mRegistrationBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
                boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
                if (sentToken) {
                    // TODO token sent to server
                } else {
                    // TODO show error that token not sent to server
                }
            }
        };

        if (checkPlayServices()) {
            // Start IntentService to register this application with GCM.
            Intent intent = new Intent(this, RegistrationIntentService.class);
            startService(intent);
        }

        InitializeScreen();
    }
    private boolean checkPlayServices() {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i("Splash screen activity", "This device is not supported.");
                finish();
                }
            return false;
        }
        return true;
    }

    @Override
    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
    }

    private void InitializeScreen() {
        new LoadDataBase(SplashScreenActivity.this).execute(SplashScreenActivity.this);
    }

    private class LoadDataBase extends AsyncTask<Context, Void, Void> {
        Context context;

        public LoadDataBase(Context context){
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Context... arg0) {
            // Create data base from assets folder.
            DatabaseHelper databaseHelper = new DatabaseHelper(arg0[0]);
            try {
                databaseHelper.createDataBase();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Closing the Data base.
            databaseHelper.close();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pauseSplashScreen(context);
        }
    }

    public void pauseSplashScreen(final Context context) {
        // New Thread call.
        new Thread() {
            // Running Thread.
            public void run() {
                int count = 0;
                while (count < 5) {
                    try {
                        Thread.sleep(1000);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                    count++;
                }

                Intent intent = new Intent(context, Activity2.class);
                startActivity(intent);
                finish();
            }
        }.start();
    }
}

问题是:我在启动屏幕之前的应用启动时出现白屏,可能是由于上述链接中的推送通知注册。

我应该怎么做以避免白屏。请指导我。

2 个答案:

答案 0 :(得分:0)

您正在使用私有内部类来创建数据库。然后从那里调用pauseSplashScreen(context)。然后开始一个新线程?为什么呢?

我建议给异步任务它自己的类带有一个接口,以便向你的通话活动发回信号。

如果您使用

Intent intent = new Intent(context, Activity2.class);
startActivity(intent);

您不必调用finish(),该调用最好用于完成当前活动并返回上一个活动。 (我让你使用一个线程来阻止你运行的其余应用程序,让这个线程回到你身边然后转移到下一个Activity是不是更好?)

所以,为了回答你的问题,我的猜测是你的Activity的启动需要一些时间,你看到的是加载的blanc布局。

答案 1 :(得分:0)

这只是因为您使用的是调试版本,当您更改为发布版本时,它将不再嘲笑它!