使用JavaScript解析XML时,是否可以显示进度对话框和错误消息?

时间:2012-03-29 20:32:01

标签: javascript android html android-webview progressdialog

我正在为像网站这样的博客开发android web应用程序。 为此,我将显示HTML页面内容列表,这些类别在单击时会显示与该类别相关的文章。

我从网站的RSS源中获取这些文章,这些RSS源是XML格式,使用JavaScript我正在解析它以显示在HTML页面上。

解析XML的过程需要花费大量时间来加载页面。在此期间我得到了空白屏幕。我已经实现了进度对话框,当页面第一次加载时,但是当JavaScript被JavaScript解析时,它可以正常工作它没有出现。

以下是我实现Process对话框的方法。 Activity.java:

@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
      this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.main);

        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                Window.PROGRESS_VISIBILITY_ON);

        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setBackgroundColor(0);

        final ProgressDialog progressDialog = new ProgressDialog(activity);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("Loading...please wait");
        progressDialog.setCancelable(true);
        webview.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }

        });

        webview.loadUrl("file:///android_asset/HomePage.html");

        // WebChromeClient give progress etc info
        webview.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view, int progress)
            {
                progressDialog.show();
                progressDialog.setProgress(0);
                activity.setProgress(progress * 1000);
                progressDialog.incrementProgressBy(progress);
                if (progress == 100 && progressDialog.isShowing())
                    progressDialog.dismiss();
            }
        });
}
  1. 如何在JavaScript解析XML时显示进度对话框?
  2. 此外,如果没有可用于同一事物的互联网连接,我想显示错误消息吗?
  3. 我使用了呼叫功能作为“tel:电话号码”,但是在我添加了de之后         public boolean shouldOverrideUrlLoading它不起作用?我做错了什么?

1 个答案:

答案 0 :(得分:2)

您的问题您可以在activity.java文件中使用以下代码

   package com.package name;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
public class Myactivity extends Activity {
    TextView myLabel;
    WebView wv;
    final Activity activity=this;
    /** Called when the activity is first created. */





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.main);

        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,Window.PROGRESS_VISIBILITY_ON);

        wv=(WebView)findViewById(R.id.webview);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setBackgroundColor(0);

        final ProgressDialog progressDialog = new ProgressDialog(activity);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("Loading...please wait");
        progressDialog.setCancelable(true);
        wv.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                 if (url.startsWith("tel:")) { 
                     Intent intent = new Intent(Intent.ACTION_DIAL,
                             Uri.parse(url)); 
                     startActivity(intent); 
                     return true;
             }else{
                view.loadUrl(url);
                return true;
            }

            }
        });

        wv.loadUrl("file:///android_asset/page.html");

        // WebChromeClient give progress etc info
        wv.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view, int progress)
            {
                progressDialog.show();
                progressDialog.setProgress(0);
                activity.setProgress(progress * 1000);
                progressDialog.incrementProgressBy(progress);
                if (progress == 100 && progressDialog.isShowing())
                progressDialog.dismiss();
            }
        }); 

        if (AppStatus.getInstance(this).isOnline(this)) {

            Toast t = Toast.makeText(this,"Welcome !!!!",8000);
            t.show();
            } else {  
                AlertDialog alertDialog = new AlertDialog.Builder(
                        CafeNashikActivity.this).create();

        // Setting Dialog Title
        alertDialog.setTitle("No Internet");

        // Setting Dialog Message
        alertDialog.setMessage("Internet Connection not available!");

        // Setting Icon to Dialog
        //alertDialog.setIcon(R.drawable.tick);

        // Setting OK Button
        alertDialog.setButton("Exit", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    MyActivity.this.finish();

                }
        });

        // Showing Alert Message
        alertDialog.show();  
            }


    }


}

希望这会对你有所帮助。