在webview加载时显示启动画面

时间:2013-02-13 19:00:30

标签: android android-intent android-webview

我想在webview加载时显示启动画面。在这段代码中,它等待超时时间(30秒),然后显示webview。我不知道我错过了什么?

public class MainActivity extends Activity {


    public static Object SPLASH_LOCK = new Object();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        WebView mywebview = (WebView) findViewById(R.id.webview);

         WebSettings webSettings = mywebview.getSettings();
         webSettings.setJavaScriptEnabled(true);
         mywebview.setVerticalScrollBarEnabled(false);
         mywebview.setHorizontalScrollBarEnabled(false);
         mywebview.getSettings().setLoadWithOverviewMode(true); 
         mywebview.getSettings().setUseWideViewPort(true);
         mywebview.getSettings().setRenderPriority(RenderPriority.HIGH);
         mywebview.setWebViewClient(new WebViewClient());
         mywebview.loadUrl("http://www.xxx.com/mobile/index.php?ver=1");

          startActivity(new Intent(this, SplashActivity.class));
    }
    public void onPageFinished (WebView view, String url) {
        synchronized (SPLASH_LOCK) {
            SPLASH_LOCK.notifyAll();
        }
    }    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        WebView mywebview = (WebView) findViewById(R.id.webview);
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
            mywebview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }




}

这是Splash Activity

public class SplashActivity extends Activity {


    private static String TAG = SplashActivity.class.getName();
    private static long MAX_SPLASH_TIME = 30000;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          this.requestWindowFeature(Window.FEATURE_NO_TITLE);    // Removes title bar
          this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);    // Removes notification bar

          setContentView(R.layout.splash);

          new Thread() {
              @Override
              public void run() {
                  synchronized (MainActivity.SPLASH_LOCK) {
                      // wait for notify or time-out
                      try { MainActivity.SPLASH_LOCK.wait(MAX_SPLASH_TIME); }
                      catch (InterruptedException ignored) {}
                  }
                  finish();
              }
          }.start();



       }



    }

3 个答案:

答案 0 :(得分:1)

永远不会调用来自onPageFinished的方法MainActivity。尝试扩展WebViewClient类并覆盖onPageFinished。然后在这里创建这个类的对象

mywebview.setWebViewClient(new CustomWebViewClient());

答案 1 :(得分:1)

您可以设置主要活动的背景,它将使用相同的

答案 2 :(得分:0)

@ abc667的含义是你必须以这种方式扩展WebViewClient以便它被调用:

webView.setWebViewClient(new WebViewClient(){

@Override
public void onPageFinished (WebView view, String url) {
        synchronized (SPLASH_LOCK) {
            SPLASH_LOCK.notifyAll();
        }
    }   
}