Android 手机app 嵌入网页操作

1、主Activity的源代码:

package com.jhtg.happyagent;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jhtg.view.SendingProgressDialog;

@SuppressLint("SetJavaScriptEnabled")
public class HomeActivity extends Activity {
    
    private WebView myWebView = null;
    private SendingProgressDialog m_progressDialog;

    /**
     * 获取WebView视图,并设置WebViewClient用来在你的WebView中打开链接
     */
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示是程序的标题栏
        setContentView(R.layout.home);
        
        myWebView = (WebView) findViewById(R.id.homeView);
        
        CookieManager.getInstance().removeAllCookie();
        WebSettings webSettings = myWebView.getSettings();//获得webSettings设置对象
        //下面3行是WebView支持JS并能够和JS代码间进行交互的设置
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setPluginState(WebSettings.PluginState.ON);//这里是支持flash的相关设置 

        myWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        myWebView.getSettings().setSupportMultipleWindows(true);

        MyWebViewClient mWebClient = new MyWebViewClient();
        myWebView.setWebViewClient(mWebClient);//向WebView提供一个WebViewClient,这样就可以在你的WebView中打开链接了
        /*myWebView.setWebChromeClient(new myChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                setProgress(progress * 100);
            }
        });*/
        myWebView.loadUrl("http://www.taobao.com");
        //myWebView.loadUrl("file:///android_asset/test.html");//包含本地文件,在WebView中打开
    }
    
    /*class myChromeClient extends WebChromeClient {
        public void onReceivedTitle(WebView view, final String title) {
            //随时更新网页的title
        }
    }*/
    
    private class MyWebViewClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.indexOf("tel:") >= 0) {// 页面上有数字会导致系统会自动连接电话,屏蔽此功能
                return true;
            }
            view.loadUrl(url);
            return true;
        }

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

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            try {
                initSendingDialog("正在加载网页,请稍后...");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    protected void initSendingDialog(String message) {
        // TODO Auto-generated method stub
        if (m_progressDialog == null) {
            m_progressDialog = new SendingProgressDialog(this, message);
        }
        m_progressDialog.start();
    }
    
    /**
     * 按键响应,在WebView中查看网页时,按返回键的时候按浏览历史退回,如果不做此项处理则整个WebView返回退出
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
        {
            // 返回键退回
            myWebView.goBack();//返回上一个历史网页
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

 

2、home.xml 的源码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/homeView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

3、SendingProgressDialog 自定义弹出框类的源码如下:

 

package com.jhtg.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.KeyEvent;

/**
 * @author sang
 * @version 创建时间:2014-7-10 上午9:25:00 类说明
 */
public class SendingProgressDialog {
    public ProgressDialog dialog;
    private final String message = "正在发送中,请稍候...";

    public SendingProgressDialog(Context paramContext) {
        this(paramContext, null);
    }

    public SendingProgressDialog(Context paramContext, String paramString) {

        dialog = new ProgressDialog(paramContext){

            @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {
                if(this.isShowing()){
                    return false;
                }
                return super.onKeyDown(keyCode, event);
            }
            
        };
        this.dialog.setCancelable(true);
        if (paramString == null) {
            this.dialog.setMessage(message);
        } else {
            this.dialog.setMessage(paramString);
        }
    }

    public void start() {
        try {
            this.dialog.show();
        } catch (Exception e) {
        }

    }

    public void stop() {
        try {
            if (this.dialog != null) {
                this.dialog.dismiss();
                this.dialog.cancel();
            }
        } catch (Exception e) {
        }
    }
}

 

最后记得要在 AndroidManifest.xml 添加权限 :

<uses-permission android:name="android.permission.INTERNET" />

 

 

Android 手机app 嵌入网页操作,布布扣,bubuko.com

Android 手机app 嵌入网页操作

上一篇:Android开发:实时处理摄像头预览帧视频------浅析PreviewCallback,onPreviewFrame,AsyncTask的综合应用(转)


下一篇:iOS应用程序生命周期(前后台切换,应用的各种状态)详解