如何在加载Android应用程序时删除白屏?

时间:2013-01-28 18:25:23

标签: android android-layout

我是android新手。我有一个加载URL的Web视图。问题是,在我打开应用程序后,在加载Web视图的URL之后有一个白色屏幕,持续2-3秒。

我认为这是应用程序启动的时间。如何删除白色屏幕并显示我的Logo? 我听说过闪屏,但是徽标出现1秒钟,然后再出现白色屏幕2-3秒,最后加载了网页视图。

我做错了什么?在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();
          }
       }
    }

androidManifest

<?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>

2 个答案:

答案 0 :(得分:4)

在你的清单中宣布两个发布活动是非常奇怪的。您应该删除SplashActivity的意图过滤器,然后从SplashActivity的{​​{1}}内开始onCreate

编辑已更新,可在等待时更换睡眠状态。加载页面或发生超时时,启动画面将消失,以先到者为准。

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

P.S。您没有询问此问题,但如果将其添加到清单中的IntentLauncher标记:

SplashActivity

然后您无需在android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 中调用requestWindowFeature或设置窗口标记。

答案 1 :(得分:2)

现在,您正在显示启动,睡眠,然后在启动主要活动(即开始加载页面)的同时解除启动。 Splash-&gt; Sleep-&gt; Launch&amp;解雇Splash - &gt;加载页面

您应该在显示启动时开始加载页面,并在页面加载时删除启动。

您可以覆盖

public void onPageFinished (WebView view, String url)
连接到 WebView WebViewClient

并使该方法解除了启动。 将flash作为Dialog而不是活动可能更容易。