在启动画面后,出现一个白色的空白屏幕,然后加载webview

时间:2013-02-01 09:33:16

标签: android android-webview splash-screen

在我的应用程序中,有两个活动。第一个是Splashscreen,第二个是Webview活动。 在启动画面显示之后,我想显示我的webview活动。但是在启动画面后,2-3秒会出现一个空白的白色屏幕然后加载webview活动。任何想法如何忽略这个白色屏幕。 我在许多帖子中研究了这个问题的解决方案但没有成功。 任何帮助将不胜感激。

添加代码: Splashscreen活动:

@Override
protected void onStart() {
    super.onStart();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            finish();
            startActivity(new Intent(SplashScreen.this,
                    WebViewActivity.class));
        }
    }, DELAY);
}

3 个答案:

答案 0 :(得分:1)

工作代码

public class WebActivity extends Activity {

protected boolean _active = true;

protected int _splashTime = 3000;

Thread splashTread;

private boolean stop = false;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while(_active && (waited < _splashTime)) {
                    sleep(100);
                    if(_active) {
                        waited += 100;
                    }
                }

            } catch(InterruptedException e) {
                // do nothing
            } finally {

                if(!stop){
                    startActivity(new Intent(WebActivity.this,Home.class));
                    finish();
                }
                else
                    finish();
            }
        }

    };
    splashTread.start();

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {

        if(splashTread.isAlive())
            this.stop = true;
    }
    return true;
}
}

答案 1 :(得分:0)

完成启动画面后,网页加载需要一些时间。这就是为什么你看到一个空白的白色屏幕出现。 Web视图开始加载进度条后。 试试这个链接。这对你有所帮助。 http://www.technotalkative.com/android-load-webview-with-progressbar/

答案 2 :(得分:0)

创建两个类文件,如WebActivity.java,home.java。

<强> WebActivity.java

       Handler handler = new Handler();

    // run a thread after 2 seconds to start the home screen
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {

            // make sure we close the splash screen so the user won't come
            // back when it presses back key

            finish();
            // start the home screen

            ProgressDialog pd = new ProgressDialog(WebActivity.this);

            //use this before calling intent
                          pd.setMessage("Processing...");

            pd.show();



            Intent intent = new Intent(WebActivity.this, Home.class);

            WebActivity.this.startActivity(intent);

        }

    }, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called

<强> Home.java

   webview=(WebView)findViewById(R.id.webView1);
webview.setWebViewClient(new myWebClient());

webview.loadUrl("http://www.google.com");}

 public class myWebClient extends WebViewClient

 {

     @Override

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            // TODO Auto-generated method stub


            view.loadUrl(url);

            return true;

        }
 }

Android清单

只需添加活动类并设置加载webview的互联网权限

即可