-3

我想加载一个网站(在 WebView 中),但加载速度太慢并显示白屏。我想显示一个启动画面并在后台加载 WebView。几秒钟后,我想关闭启动屏幕并显示到那时应该准备好的站点。我该怎么做?谢谢!

4

4 回答 4

1

我将此代码用于启动画面

public class SplashActivity extends Activity {

    private boolean isBackButtonPressed;
    private static final int SPLASH_DURATION = 2000; // 2 seconds

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTheme(R.style.SplashTheme);
        getWindow().setWindowAnimations(0);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_splash);

        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();

                if (!isBackButtonPressed) {
                    // start the home screen if the back button wasn't pressed
                    Intent intent = new Intent(SplashActivity.this, FragmentActivity.class);
                    startActivity(intent);
                    finish();   
                }

            }

        }, SPLASH_DURATION); 
    }

    @Override
   public void onBackPressed() {
        super.onBackPressed();
        isBackButtonPressed = true;
    }   

}
于 2013-04-03T06:40:46.950 回答
1

您可以使用以下代码。

public class Splash extends Activity {
/** Called when the activity is first created. */

private boolean mSplashActive = true, mPaused;
private long mSplashTime = 1000;

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

    new Thread(){
        public void run(){
            try {
                long ms = 0;
                while(mSplashActive && ms < mSplashTime) {
                    sleep(100);
                    if(!mPaused) {
                        ms += 100;
                    }
                }
                if(Resources.getResources().isUserRegistered(Splash.this)) {
                    //user is registered so launch main screen
                } else {
                    //user is not registered launch welcome wizard.
                    Intent intent = new Intent(Splash.this, WelcomeScreen.class);
                    Splash.this.startActivity(intent);
                }
                finish();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }.start();

}

@Override
public void onPause(){
    super.onPause();
    mPaused = true;
}

@Override
public void onResume(){
    super.onResume();
    mPaused = false;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);

    if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        mSplashActive = false;
    }
    return true;
}
}
于 2013-04-03T06:43:48.060 回答
0

在 webview 类中,您可以设置类似的内容。

我为你设置了这个:

public class WebViewActivity extends Activity {

WebView webView;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);
    progressDialog = new ProgressDialog(WebViewActivity.this);
    progressDialog.setTitle("here your title");
    progressDialog.setMessage("message here");

    webView = (WebView) findViewById(R.id.webView1);

    webView.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            // here you can display an imageview fullscreen
            // or show and progressdialog
            progressDialog.show();

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            // here you can remove the imageview and the user can continue using the webview
            // or hide the progressdialog
            progressDialog.dismiss();

        }

    });

}

}

如果您有任何问题,请告诉我

于 2013-04-03T06:47:03.897 回答
0

公共类 SplashScreen 扩展 Activity { 私有线程 mSplashThread;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash);

    final SplashScreen sPlashScreen = this;

    // The thread to wait for splash screen events
    mSplashThread = new Thread() {
        @SuppressWarnings("deprecation")
        @Override
        public void run() {
            try {
                synchronized (this) {
                    // Wait given period of time or exit on touch
                    wait(5000);
                }
            } catch (InterruptedException ex) {
            }

            finish();

            // Run next activity
            Intent intent = new Intent();
            intent.setClass(sPlashScreen, MainActivity.class);
            startActivity(intent);

        }
    };

    mSplashThread.start();
}

/**
 * Processes splash screen touch events
 */
@Override
public boolean onTouchEvent(MotionEvent evt) {
    if (evt.getAction() == MotionEvent.ACTION_DOWN) {
        synchronized (mSplashThread) {
            mSplashThread.notifyAll();
        }
    }
    return true;
}

}

于 2013-04-03T07:06:21.823 回答