1

我的给定 URL 没有加载此代码,但是如果我按“主页”键并再次单击应用程序图标(即恢复我的活动),那么它会加载。我尝试查看活动生命周期,修改了代码,但仍然无法正常工作。如果我需要进一步澄清我的问题,请告诉我。谢谢。

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.http.SslError;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView.SavedState;
import android.widget.Toast;

import com.ABC.R;
import com.ABC.util.CustomProgressDialogs;
import com.ABC.util.DeviceCheck;
import com.ABC.util.GlobalFunction;
import com.ABC.util.HTML5WebView;
import com.ABC.util.KeyBoard;
import com.ABC.util.NetworkCheck;

public class CanvasActivity extends Activity {

    private HTML5WebView mWebView;
    private ProgressDialog webPageDialog;
    private Context context;
    private GlobalFunction globalFunc;
    private KeyBoard keyBoard;
    private BroadcastReceiver connectionReceiver;
    String myUrl;
    SharedPreferences prefs;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to exit?")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    Log.d("killapp", "killapp");
                                    globalFunc.killApplication(context);
                                }
                            })
                    .setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });
            AlertDialog exitDialog = builder.create();
            exitDialog.show();
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = CanvasActivity.this;
        mWebView = new HTML5WebView(context);
        // setContentView(mWebView.getLayout());
        final DeviceCheck deviceCheck = new DeviceCheck(context); // change
        // mWebView = new HTML5WebView(this);
        // getWindow().requestFeature(Window.);
        // setContentView()
        // getWindow().
        setContentView(mWebView.getLayout());
        globalFunc = new GlobalFunction();
        webPageDialog = new ProgressDialog(context);

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Log.d("onReceivedError", failingUrl + " " + errorCode);
                mWebView.loadUrl("file:///android_asset/myerrorpage.html");

            }

            @Override
            public void onReceivedSslError(WebView view,
                    SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                Log.d("onReceivedSslError", "ssl error");
                handler.proceed();
                // To supercede SSL certificate error.
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.d("page started", "page loading started");
                webPageDialog.show();
                webPageDialog.setContentView(CustomProgressDialogs
                        .iphoneProgressDialog(context));
                webPageDialog.setCancelable(true);

                // change

                webPageDialog.setOnCancelListener(new OnCancelListener() {

                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub
                        webPageDialog.dismiss();
                        globalFunc.killApplication(context);
                    }
                });

            }

            @Override
            public void onPageFinished(final WebView view, final String url) {
                Log.i("onPageFinished", "onPageFinished");
                if (webPageDialog.isShowing()) {
                    webPageDialog.dismiss();
                }
            }
        });

        if (getIntent().getStringExtra("Message") != null) {
            Toast.makeText(getApplicationContext(),
                    getIntent().getIntExtra("push details", 0) + "",
                    Toast.LENGTH_LONG).show();
        }

        keyBoard = new KeyBoard(mWebView, context);
        mWebView.addJavascriptInterface(keyBoard, "KeyBoard");

        prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        /*
         * if(prefs.getString("url",
         * getResources().getString(R.string.MYURL_URL)).trim().equals("")){
         * myUrl=getResources().getString(R.string.MYURL_URL).trim(); }else{
         * myUrl = prefs.getString("url",
         * getResources().getString(R.string.MYURL_URL)).trim(); }
         * mWebView.loadUrl(myUrl);
         */
        myUrl = getResources().getString(R.string.MYURL_URL).trim();
        /*
         * if (!myUrl.equalsIgnoreCase(prefs.getString("url",
         * getResources().getString(R.string.MYURL_URL)).trim())) {
         * 
         * myUrl = prefs.getString("url",
         * getResources().getString(R.string.MYURL_URL)).trim();
         * 
         * }
         */
        // mWebView.loadUrl(myUrl);

        if (new NetworkCheck(context).isOnline()) {

            // change

            webPageDialog.show();
            webPageDialog.setContentView(CustomProgressDialogs
                    .iphoneProgressDialog(context));
            webPageDialog.setCancelable(true);
            webPageDialog.setOnCancelListener(new OnCancelListener() {

                public void onCancel(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    webPageDialog.dismiss();
                    globalFunc.killApplication(context);
                }
            });

            if (savedInstanceState != null) {
                mWebView.restoreState(savedInstanceState);
            } else {
                mWebView.loadUrl(myUrl);
                /*
                 * if (deviceCheck.isTablet()) {
                 * setRequestedOrientation(ActivityInfo
                 * .SCREEN_ORIENTATION_LANDSCAPE); mWebView.loadUrl(myUrl); }
                 * else {
                 * setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                 * ); mWebView.loadUrl(myUrl); }
                 */
            }
        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
            mWebView.loadUrl(myUrl);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!((prefs.getString("url",
                getResources().getString(R.string.MYURL_URL)).trim()))
                .equals("")) {
            myUrl = prefs.getString("url",
                    getResources().getString(R.string.MYURL_URL)).trim();
        }
        if (!(myUrl.equals(mWebView.getUrl()))) {
            // mWebView.loadUrl(myUrl);
        }

        /*
         * if (!mWebView.getUrl().equalsIgnoreCase(prefs.getString("url",
         * getResources().getString(R.string.MYURL_URL)).trim())) {
         * 
         * myUrl = prefs.getString("url",
         * getResources().getString(R.string.MYURL_URL)).trim();
         * mWebView.loadUrl(myUrl); }
         */
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mWebView.saveState(outState);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d("CanvasActivity-onOptionsItemSelected", "in option selected");
        switch (item.getItemId()) {
        case R.id.url:
            startActivity(new Intent(this, EditURL.class));
            return true;
        }
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        Log.d("CanvasActivity-onCreateOptionsMenu",
                "in oncreate option menu");
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menuxml, menu);
        return true;
    }

    @Override
    public void onStop() {
        super.onStop();
        mWebView.stopLoading();
    }

    // onkeydown was here.

    public void startBroadcastService() {
        connectionReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo mWifi = connManager
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                ConnectivityManager connManager1 = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo mMobile = connManager1
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                boolean isNoNetwork = intent.getBooleanExtra(
                        ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                if (!isNoNetwork) {
                    if (mWifi.isAvailable()) {
                    }
                    if ((mWifi.isConnected() && mWifi.isAvailable())
                            || (mMobile.isConnected() && mMobile.isAvailable())) {
                        if (mWebView.getUrl().contains("myerrorpage")) {
                            mWebView.loadUrl(myUrl);
                        }
                    }
                }
            }
        };
        registerReceiver(connectionReceiver, new IntentFilter(
                "android.net.conn.CONNECTIVITY_CHANGE"));
    }
}
4

1 回答 1

0

在我的 webview 使用相关函数(onPageStarted onPageFinished 和 onReceivedError)在该处理程序中注册后,我创建了一个处理程序并加载了我的 URL,因为我的 webview 需要很长时间来加载页面。这行得通。

  handler = new Handler();
                jsInterface = new JSInterface(mWebView, context, uniqueGeneratedkey,
                        handler);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        jsInterface.initiateProcess();
    /*where initiateProcess() is the method where I register all the overridden
     *functions ie. OnPageStarted() and OnPageFinished();
     */   
                    }
                });
    mWebview.LoadUrl(url);
于 2013-01-04T11:49:10.773 回答