【问题标题】:how can i load 2 activities, show the first and after faw seconds the second? [duplicate]我如何加载 2 个活动,显示第一个,然后在第一秒后显示第二个? [复制]
【发布时间】:2013-04-03 06:36:35
【问题描述】:

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

【问题讨论】:

  • 一般来说,最好显示您目前拥有的代码,如果只是为了避免您的问题被否决!

标签: android


【解决方案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;
    }   

}

【讨论】:

    【解决方案2】:

    您可以使用以下代码。

    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;
    }
    }
    

    【讨论】:

      【解决方案3】:

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

      }

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

      【讨论】:

      • 非常感谢!你的回答是我最需要的!但我正在使用 Chrome - ( mWebView.setWebChromeClient(new GeoWebChromeClient(){ ) ,并且没有像“onPageFinished”这样的选项...... :-(
      • 您可以同时设置 Webchromeclient 和 webviewclient,所以无论您设置 webchrome 还是 webview 或两者都设置。
      • 对于我建议设置的进度对话框:progressDialog.setCancelable(false);和progressDialog.setCanceledOnTouchOutside(false);这样用户就不能取消(关闭)对话框。
      【解决方案4】:

      公共类 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;
      }
      

      }

      【讨论】:

        猜你喜欢
        • 2014-01-04
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 2017-08-06
        • 2018-05-20
        • 2022-08-13
        • 2011-11-24
        • 1970-01-01
        相关资源
        最近更新 更多