一、源码简介
本项目小木人印象安卓手机APP源码包,基于Android Studio IDE的gradle android app项目,基于webview控件通过WebChromeClient浏览器引擎来实现手机官网访问,apk二维码如下所示
二、源码分析
1.基于android.webkit.WebChromeClient的示例代码MyWebChromeClient
import android.app.AlertDialog;@b@import android.content.DialogInterface;@b@import android.content.DialogInterface.OnClickListener;@b@import android.content.DialogInterface.OnCancelListener;@b@import android.webkit.JsPromptResult;@b@import android.webkit.JsResult;@b@import android.webkit.WebChromeClient;@b@import android.webkit.WebView;@b@import android.widget.EditText;@b@@b@public class MyWebChromeClient extends WebChromeClient {@b@@b@ public MyWebChromeClient() {@b@ // TODO Auto-generated constructor stub@b@ }@b@@b@ @Override@b@ public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {@b@ AlertDialog.Builder b2 = new AlertDialog.Builder(view.getContext())@b@ .setTitle(R.string.app_name)@b@ .setMessage(message)@b@ .setPositiveButton("ok",@b@ new AlertDialog.OnClickListener() {@b@ @Override@b@ public void onClick(DialogInterface dialog,@b@ int which) {@b@ result.confirm();@b@ //result.cancel();@b@ }@b@ });@b@@b@ b2.setCancelable(false);@b@ b2.create();@b@ b2.show();@b@@b@ return true;@b@ }@b@@b@ @Override@b@ public boolean onJsConfirm(WebView view, String url, String message,@b@ final JsResult result) {@b@ final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());@b@ builder.setTitle(R.string.app_name).setMessage(message)@b@ .setPositiveButton("确定", new OnClickListener() {@b@@b@ @Override@b@ public void onClick(DialogInterface dialog, int which) {@b@ result.confirm();@b@@b@ }});@b@@b@ builder.setNeutralButton("取消", new OnClickListener() {@b@ @Override@b@ public void onClick(DialogInterface dialog, int which) {@b@ result.cancel();@b@ }@b@@b@ });@b@ builder.setOnCancelListener(new OnCancelListener() {@b@ @Override@b@ public void onCancel(DialogInterface dialog) {@b@ result.cancel();@b@ }@b@ });@b@@b@ builder.create();@b@ builder.show();@b@@b@ return true;@b@ }@b@@b@ @Override@b@ public boolean onJsPrompt(WebView view, String url, String message,@b@ String defaultValue, final JsPromptResult result) {@b@ final AlertDialog.Builder builder = new AlertDialog.Builder(view@b@ .getContext());@b@ builder.setTitle(R.string.app_name).setMessage(message);@b@ final EditText et = new EditText(view.getContext());@b@ et.setSingleLine();@b@ et.setText(defaultValue);@b@ builder.setView(et);@b@ builder.setPositiveButton("确定", new OnClickListener() {@b@ @Override@b@ public void onClick(DialogInterface dialog, int which) {@b@ result.confirm(et.getText().toString());@b@ }@b@ }).setNeutralButton("取消", new OnClickListener() {@b@ @Override@b@ public void onClick(DialogInterface dialog, int which) {@b@ result.cancel();@b@ }@b@ });@b@@b@ builder.create();@b@ builder.show();@b@@b@ return true;@b@ }@b@@b@}
2. WebviewDemo代码示例
import android.app.Activity;@b@import android.app.AlertDialog;@b@import android.content.Context;@b@import android.content.DialogInterface;@b@import android.content.DialogInterface.OnCancelListener;@b@import android.graphics.Bitmap;@b@import android.os.Bundle;@b@import android.util.Log;@b@import android.view.View;@b@import android.view.WindowManager;@b@import android.view.View.OnClickListener;@b@import android.view.Window;@b@import android.webkit.JsPromptResult;@b@import android.webkit.JsResult;@b@import android.webkit.WebChromeClient;@b@import android.webkit.WebSettings;@b@import android.webkit.WebView;@b@import android.webkit.WebSettings.ZoomDensity;@b@import android.webkit.WebViewClient;@b@import android.widget.TextView;@b@@b@import android.webkit.CookieManager;@b@import android.webkit.CookieSyncManager;@b@@b@public class WebviewDemo extends Activity {@b@ private WebView mWebview;@b@// private Object mJsInterface = new JsInterface();@b@ private Context mContext;@b@@b@ /** Called when the activity is first created. */@b@ @Override@b@ public void onCreate(Bundle savedInstanceState) {@b@ super.onCreate(savedInstanceState);@b@ @b@ requestWindowFeature(Window.FEATURE_NO_TITLE);@b@ getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);@b@ setContentView(R.layout.main);@b@@b@ mWebview = (WebView)findViewById(R.id.webview);@b@ @b@ WebSettings settings = mWebview.getSettings();@b@ settings.setCacheMode(MODE_PRIVATE);@b@ settings.setBuiltInZoomControls(false);@b@ settings.setSupportZoom(false);@b@ settings.setDefaultZoom(ZoomDensity.FAR);@b@ settings.setJavaScriptEnabled(true);@b@ settings.setAppCacheEnabled(false);@b@ settings.setPluginsEnabled(true);@b@@b@ settings.setUseWideViewPort(true);@b@ settings.setLoadWithOverviewMode(true);@b@ settings.setUseWideViewPort(true);@b@@b@// mWebview.addJavascriptInterface(mJsInterface, "demo");@b@ mWebview.setWebChromeClient(new MyWebChromeClient());@b@ mWebview.setWebViewClient(webViewClient);@b@@b@ mWebview.loadUrl("http://www.xwood.net/index.html");@b@@b@}@b@@b@ @Override@b@ protected void onStop() {@b@ mWebview.destroy();@b@ super.onStop();@b@ }@b@@b@/* private class JsInterface {@b@ @SuppressWarnings("unused")@b@ public void changePage(final int to){@b@ WebviewDemo.this.runOnUiThread(new Runnable(){@b@ @Override@b@ public void run() {@b@ if(1==to){@b@ @b@ }else if(2==to){@b@ @b@ }@b@ }});@b@ }@b@ @b@ }*/@b@ @b@ private WebViewClient webViewClient = new WebViewClient() {@b@@b@ public boolean shouldOverrideUrlLoading(WebView view, String url) {@b@ Log.e("ttttt",url);@b@ return false;@b@ }@b@ @b@ @Override@b@ public void onReceivedError(WebView view, int errorCode,@b@ String description, String failingUrl) {@b@ super.onReceivedError(view, errorCode, description, failingUrl);@b@ }@b@@b@ public void onPageStarted(WebView view, String url, Bitmap favicon) {@b@ super.onPageStarted(view, url, favicon);@b@ };@b@@b@ public void onPageFinished(WebView view, String url) {@b@ super.onPageFinished(view, url);@b@ }@b@ };@b@@b@}