减少在WebView中显示进度栏的时间

时间:2018-09-04 12:17:37

标签: android android-webview android-progressbar

我在减少在WebView中显示进度栏的时间方面遇到问题。用于低估添加图像。 enter image description here

当我的网站在WebView中运行时,我可以在哪里实现某些线程停止显示进度条一秒钟?

在onPageStarted中显示进度条之前,我尝试过实现线程,但是它等待整个加载页面,不仅是在加载进度条。

我们在网页上的加载速度非常慢,因此我们需要在没有进度条的webview中向用户显示一到两秒钟的加载页面,此后显示“正在加载”进度条。

如果您有任何疑问,请问我,我们需要快速答复以解决此问题。我正在尝试找到解决方案,但没有解决办法。

非常感谢!

有我的ChromeClien.class

public class MyWebChromeClient extends WebChromeClient {
private ProgressListener mListener;

public MyWebChromeClient(ProgressListener listener) {
    mListener = listener;
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
    mListener.onUpdateProgress(newProgress);
    super.onProgressChanged(view, newProgress);
}
public interface ProgressListener {
    public void onUpdateProgress(int progressValue);
}}

有MainActivity.class

public class MainActivity extends AppCompatActivity{

private WebView webview;
ProgressDialog prDialog;
String cekani;

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

    CookieManager.getInstance().setAcceptCookie(true);

    Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isFirstRun", true);

    if (isFirstRun) {
        //show start activity
        startActivity(new Intent(MainActivity.this, PrvniSpusteni.class));

    }

    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
            .putBoolean("isFirstRun", false).commit();

    if(KontrolaInternetu.isInternetAvailable(MainActivity.this)) //Vrátí hodnotu TRUE, pokud je připojení k internetu k dispozici
    {
        webview =(WebView)findViewById(R.id.webView);
        //Nastavení webové stránky
        webview.loadUrl(getString(R.string.url_aplikace));
        webview.setWebViewClient(new MyWebViewClient());
        //Puštění JavaScriptu pro web
        webview.getSettings().setJavaScriptEnabled(true);
      webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        WebSettings ws = webview.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setAllowFileAccess(true);

        if (android.os.Build.VERSION.SDK_INT >= 21) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webview, true);
        }else {
            CookieManager.getInstance().setAcceptCookie(true);
        }

        webview.setWebChromeClient(new WebChromeClient(){
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                request.grant(request.getResources());
            }
        });

        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ECLAIR) {
            try {
                Log.d(TAG, "Enabling HTML5-Features");
                Method m1 = WebSettings.class.getMethod("setDomStorageEnabled", new Class[]{Boolean.TYPE});
                m1.invoke(ws, Boolean.TRUE);

                Method m2 = WebSettings.class.getMethod("setDatabaseEnabled", new Class[]{Boolean.TYPE});
                m2.invoke(ws, Boolean.TRUE);

                Method m3 = WebSettings.class.getMethod("setDatabasePath", new Class[]{String.class});
                m3.invoke(ws, "/data/data/" + getPackageName() + "/databases/");

                Method m4 = WebSettings.class.getMethod("setAppCacheMaxSize", new Class[]{Long.TYPE});
                m4.invoke(ws, 1024*1024*8);

                Method m5 = WebSettings.class.getMethod("setAppCachePath", new Class[]{String.class});
                m5.invoke(ws, "/data/data/" + getPackageName() + "/cache/");

                Method m6 = WebSettings.class.getMethod("setAppCacheEnabled", new Class[]{Boolean.TYPE});
                m6.invoke(ws, Boolean.TRUE);

                Log.d(TAG, "Enabled HTML5-Features");
            }
            catch (NoSuchMethodException e) {
                Log.e(TAG, "Reflection fail", e);
            }
            catch (InvocationTargetException e) {
                Log.e(TAG, "Reflection fail", e);
            }
            catch (IllegalAccessException e) {
                Log.e(TAG, "Reflection fail", e);
            }
        }
    }
    else
    {
    //Zobrazení AlertDialogu pokud není připojení k internetu
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                if (!isFinishing()){
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle(getString(R.string.no_internet))
                            .setMessage(getString(R.string.internet))
                            .setCancelable(false)
                            .setIcon(R.drawable.icon)

                            .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                    System.exit(0);
                                }})
                            .setNegativeButton(getString(R.string.zrusit), new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                }})
                            .show();
                }
            }
        });
    }


}

      //Při stisknutí tlačítka zpět se uživatel vrátí ve webview nazpět bez toho, aby aplikace spadla.
private final String TAG = MainActivity.class.getSimpleName();

// Progress bar - zobrazí se tehdy, pokud čekám na načítání stránky
private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {


        super.onPageStarted(view, url, favicon);

        prDialog = new ProgressDialog(MainActivity.this);
        prDialog.setIcon(R.drawable.icon);
        prDialog.setMessage(getString(R.string.nacitani_webove_stranky));
        prDialog.show();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if(prDialog!=null){
            prDialog.dismiss();
        }
    }
}

  //Při stisknutí tlačítka zpět se uživatel dostane zpět pouze ve webview
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
            case KeyEvent.KEYCODE_BACK:
                if(webview.canGoBack() == true){
                    webview.goBack();
                }else{
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.endsWith(".mp3")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "audio/*");
        view.getContext().startActivity(intent);
        return true;
    } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "video/*");
        view.getContext().startActivity(intent);
        return true;
    } else {
    return true;
}}}

1 个答案:

答案 0 :(得分:0)

  

嘿,兄弟,您不一定需要为您实现线程   需求。首先,您可以在网络视图中加载网址,   在特定时间段,您可以开始显示进度条。对于   在一定时间后可以调用进度条   处理程序

私有类MyWebViewClient扩展了WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {


    super.onPageStarted(view, url, favicon);

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    @Override
     public void run() {

       //Do something after 100ms
       prDialog = new ProgressDialog(MainActivity.this);
       prDialog.setIcon(R.drawable.icon);
       prDialog.setMessage(getString(R.string.nacitani_webove_stranky));
       prDialog.show();

      }
     }, 100);

}

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    if(prDialog!=null){
        prDialog.dismiss();
    }
}