我是Android新手。我有一个加载url的web视图。问题是,在我打开应用程序后,在加载web视图的url后,会出现2-3秒的白色屏幕。
我想现在是申请启动的时候了。我该如何移除白色屏幕并显示我的徽标?
我听说过SplashScreen,但在这个过程中,徽标出现了1秒,然后白色屏幕出现了2-3秒,最后加载了Web视图。
我做错什么了?在加载web视图时,是使用启动屏幕还是其他方式显示徽标?

package com.exampe.dating;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

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

        WebView mywebview = (WebView) findViewById(R.id.webview);
         mywebview.loadUrl("http://www.example.com/mobile/index.php");
         WebSettings webSettings = mywebview.getSettings();
         webSettings.setJavaScriptEnabled(true);
         mywebview.setWebViewClient(new WebViewClient());
    }

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

splashActivity.java文件
package com.exampe.dating;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class SplashActivity extends Activity {

       private static String TAG = SplashActivity.class.getName();
       private static long SLEEP_TIME = 10;    // Sleep for some time

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

          // Start timer and launch main activity
          IntentLauncher launcher = new IntentLauncher();
          launcher.start();
       }

       private class IntentLauncher extends Thread {
          @Override
          /**
           * Sleep for some time and than start new activity.
           */
          public void run() {
             try {
                // Sleeping
                Thread.sleep(SLEEP_TIME*1000);
             } catch (Exception e) {
                Log.e(TAG, e.getMessage());
             }

             // Start main activity
             Intent intent = new Intent(SplashActivity.this, MainActivity.class);
             SplashActivity.this.startActivity(intent);
             SplashActivity.this.finish();
          }
       }
    }

雄激素清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exampe.dating"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
         android:name=".SplashActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>

        <activity
            android:name="com.exampe.dating.MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

最佳答案

在你的清单上有两个这样的发射活动是很奇怪的。您应该消除SplashActivity的意图过滤器,然后从SplashActivityonCreate内开始MainActivity
编辑已更新,用等待代替睡眠。加载页面或发生超时时(以先到者为准),启动屏幕将消失。

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);
        mywebview.loadUrl("http://www.example.com/mobile/index.php");
        WebSettings webSettings = mywebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebview.setWebViewClient(new WebViewClient());

        startActivity(new Intent(this, SplashActivity.class));
    }
    . . .
}

// in your WebViewClient:
public void onPageFinished (WebView view, String url) {
    synchronized (SPLASH_LOCK) {
        SPLASH_LOCK.notifyAll();
    }
}

然后在SplashActivity中,等待所需的延迟,然后调用finish()。您不需要再次启动MainActivity,因为它位于SplashActivity后面,并将在SplashActivity完成时出现。
public class SplashActivity extends Activity {

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

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

          . . .

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

注意,您不需要IntentLauncher类。
附:你没有问这个问题,但是如果你把这个添加到清单中的SplashActivity标签上:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

然后不需要调用requestWindowFeature或在onCreate中设置窗口标志。

07-27 13:28