简体   繁体   English

Android Webview的启动画面

[英]Splash Screen for Android Webview

I have a WebView app and would like to add a splash screen to show either while loading the page, or for a set amount of time. 我有一个WebView应用程序,想添加一个启动屏幕,以在加载页面时显示或显示一段固定的时间。 I have tried some other solutions but could not find one that worked. 我尝试了其他解决方案,但找不到可行的解决方案。 If necessary to know, my splash image is splash.png, located in the drawable folder. 如果有必要知道的话,我的启动图像是位于drawable文件夹中的splash.png。

Here is my MainActivity.java 这是我的MainActivity.java

public class MainActivity extends Activity {

private GoogleApiClient client;

@Override
public void onBackPressed()
{
    WebView webView = (WebView) findViewById(R.id.webView);
    if(webView.canGoBack()){
        webView.goBack();
    }
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Tap Twice To Exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;
        }
    }, 2000);
}

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


    WebView view = (WebView) this.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyBrowser());

    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    if(isNetworkStatusAvialable (getApplicationContext())) {
        view.loadUrl("file:///android_asset/index.html");
    } else {
        view.loadUrl("file:///android_asset/error.html");

    }
}
public static boolean isNetworkStatusAvialable (Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null)
    {
        NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
        if(netInfos != null)
            if(netInfos.isConnected())
                return true;
    }
    return false;
}
@Override
public void onStart() {
    super.onStart();

    client.connect();
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW,
            "MLINKS",
            Uri.parse("http://host/path"),

            Uri.parse("android-app://PACKAGE-NAME/http/host/path")
    );
    AppIndex.AppIndexApi.start(client, viewAction);
}

@Override
public void onStop() {
    super.onStop();

    Action viewAction = Action.newAction(
            Action.TYPE_VIEW,
            "MLINKS",
            Uri.parse("http://host/path"),
            Uri.parse("android-app://PACKAGE-NAME/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

boolean doubleBackToExitPressedOnce = false;

}

Thank you for any help! 感谢您的任何帮助!

EDIT: 编辑:

Define a Progress Dialog: 定义进度对话框:

    private ProgressDialog pd;

Then inside your: 然后在您的:

view.setWebViewClient(new MyBrowser() { view.setWebViewClient(new MyBrowser(){

Insert this: 插入此:

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            pd.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            pd.dismiss();
        }

I did something like this at one time. 我一次做了这样的事情。 IIRC, it went like this: IIRC,它像这样:

First, I created a special ImageView subclass that overrode onTouchEvent() and threw away all the events. 首先,我创建了一个特殊的ImageView子类,该子类覆盖了onTouchEvent()并丢弃了所有事件。

Then for my layout, I had my ImageView subclass overlay the WebView then made it hidden, like this (for example): 然后对于我的布局,我将ImageView子类覆盖在WebView然后将其隐藏,例如(如下所示):

<FrameLayout
    ...
    >

    <WebView
        ...
        />

    <com.example.TouchHandlerImageView
        ....
        android:src="R.drawable.splash"
        android:visibility="gone"
        />  
....
</FrameLayout>

Then my code went like this: 然后我的代码如下:

  • Show the splash imageview. 显示初始图像视图。 (The onTouchEvent() would keep touch events from going through to the WebView underneath) onTouchEvent()将使触摸事件不会传递到下面的WebView
  • Start the page loading (In my case, I also had to run some javascript to set up the page) 开始页面加载(就我而言,我还必须运行一些javascript来设置页面)
  • In WebViewClient onPageFinshed() , hide the splash imageview, displaying the WebView with page fully loaded. WebViewClient onPageFinshed() ,隐藏初始图像WebView ,显示页面已完全加载的WebView

at first thing make a background and show it im main screen till the data is being ready. 首先,做一个背景并在主屏幕上显示它,直到准备好数据为止。

use AsyncTask Class because it has 3 methods which them 使用AsyncTask类,因为它有3种方法

1- onPreExecute() //don't do anything here 1- onPreExecute()//在这里什么都不做

2- doInBackgroundProcess()// fetch your data here 2- doInBackgroundProcess()//在这里获取数据

3- onPostExecute()//get back the original background 3- onPostExecute()//找回原始背景

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM