android 通过webView,简单的通过网页唤起本地界面,如果比较多的话最好不要用原生的webView和js去搭建,用框架去做

文章来自:http://blog.csdn.net/intbird

http://blog.csdn.net/intbird/article/details/49078509

NOTE : 

1.--

js和android交互是在 javabrige 线程中的,这个线程的hanlder是主线程的,

也就是说需要主线程new的东西可以在这个线程中new;

2.--

Js文件中不能使用 // 做为注释,否则不读取该js;

 //弹窗提示

function appAlert(msg){

javascript.android.appAlert(msg);

}

3.--

@javascriptInterface 这个就不用说了,直接调用的方法必须加这个,如果target API版本大于17的话

@suppressLint(''SetJavaScriptEnabled")很久之前也用这个

4.--

android接受不了js中的对象,比如

java:

@javascriptInterface

public void input(Object obj){

}

@javascriptInterface

public void input(String name,String pwd){

}

js:

javascript.android.input({

user:user.name,

user:user.pwd

});

这个是接收不了的,即使只有一个obj对象;


webView设置


package com.idonoo.shareCar.ui.commom.activitys;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.idonoo.frame.GlobalInfo;
import com.idonoo.frame.beanType.IdentifyType;
import com.idonoo.frame.beanType.WebUrlType;
import com.idonoo.shareCar.R;
import com.idonoo.shareCar.app.AppEvent;
import com.idonoo.shareCar.app.IntentExtra;
import com.idonoo.shareCar.ui.main.MainSlideContent;
import com.idonoo.shareCar.uiframe.BaseActivity;
import com.idonoo.shareCar.vendor.share.ShareHelper;
import com.umeng.analytics.MobclickAgent;

public class WebViewActivity extends BaseActivity {

	private WebView webView;
	
	private WebUrlType urlType;
	private String webTitle="";
	private String webDesption="";
	private String webUrl="";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_web_view);
		initUI();
		initData();
	}

	@Override
	protected void initUI() {
		super.initUI();
		preListener=new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		};
		super.initActionBar();
		
		webView=(WebView)findViewById(R.id.webView);
	}

	@Override
	protected void initData() {
		
		webUrl=getIntent().getStringExtra(IntentExtra.EXTRA_URL);
		urlType=(WebUrlType) getIntent().getSerializableExtra(IntentExtra.EXTRA_URL_TYPE);
		
		if(TextUtils.isEmpty(webUrl)){
			finish();
			return ;
		}
		parseWebUrlType(urlType);
		
		webView.getSettings().setJavaScriptEnabled(true);
		webView.addJavascriptInterface(new JsInterfaceBody(this), "intbird");//添加和js的结合
		
		WebViewClient viewClient = new WebViewClient(){
			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				dismissProgress();
				webUrl=url;
				onPageLoadFinish(url);
			}
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				showProgress("");
				webUrl=url;
				onPageLoadStart(url);
			}
		};
		webView.setWebViewClient(viewClient);
		
		WebChromeClient client=	new WebChromeClient(){
			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				parseWebUrlTitle(title);
				setTitle(webTitle);
			}
		};
		webView.setWebChromeClient(client);
		webView.loadUrl(webUrl+"?visitTimes="+System.currentTimeMillis());
	
		webView.setDownloadListener(new DownloadListener() {
			
			@Override
			public void onDownloadStart(String url, String userAgent,
					String contentDisposition, String mimetype, long contentLength) {
				webView.goBack();
				Uri uri = Uri.parse(url);  
	            Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
	            startActivity(intent);
			}
		});
		
	}
	
	protected void onPageLoadStart(String url) {
		
	}

	protected void onPageLoadFinish(String url) {
		doMobileAgentent(url);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if(webView.canGoBack()){
			webView.goBack();
			return true;
		}else{
			Activity act = getParent();
			if(act != null&&act instanceof MainSlideContent){
				return ((MainSlideContent)getParent()).onKeyDown(keyCode, event);
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	private View.OnClickListener viewShareAction =new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			ShareHelper.Builder helper=new ShareHelper.Builder(getActivity());
			helper.setShareToAll(true).
			setTitle(webTitle).
			setContent(webDesption+webTitle+webUrl).
			setClickUrl(webUrl).
			setImageRes(R.drawable.i_png_logo_share).
			create().show();
		}
	};
	
	private void doMobileAgentent(String url) {
		if(url.contains("?")){
			try{
				url=url.substring(0,url.indexOf("?"));
			}catch(Exception ex){
			}
		}
		HashMap<String, String> maps=new HashMap<String, String>();
		maps.put("openUrl", url);
		MobclickAgent.onEvent(getActivity(), AppEvent.WEB_URLS_IN_APP,maps); 
	}
}


JsInterfaceBase

public abstract class JsInterfaceBase {
	
	public abstract void  result_response(boolean result);
	
	public abstract void  login_register();
	
	public abstract void  author_user(int auhotType,int authorResult);
	
	public abstract void  order_details_passager(String orderNo);
	
	public abstract void  order_details_driver(String orderNo);
	
	public abstract void  route_details(String orderNo);
	
	public abstract void  carneeds_publish();
	
	public abstract void  carneeds_list_main();
	
	public abstract void  carneeds_list_nearby();
}


JsInterfaceBody

private Context mContext;
	public JsInterfaceBody(Context context){
		this.mContext=context;
	}
	public JsInterfaceBody(){
		
	}
	
	private Context getActivity(){
		if(null == mContext){
			mContext = Frame.getInstance().getAppContext();
		}
		return mContext;
	}
	
	private void startActivity(Intent intent){
		getActivity().startActivity(intent);
		result_response(true);
	}
	
	@Override 	@JavascriptInterface
	public void result_response(boolean result) {
		JsInterfaceHtml.notifyResult(result);
	}
	
	@Override	@JavascriptInterface
	public void login_register() {
		Intent intent=new Intent(getActivity(),LoginWellcome.class);
		startActivity(intent);
	}

	@Override @JavascriptInterface
	public void author_user(int authorType,int authResult) {
		if(!isLogin()){
			login_register();
		}else{
			Intent intent= null;
			 switch(authResult){
			 case 1:
				 intent = new Intent(getActivity(),UserCenterActivity.class);
				 break;
			 case 2:
				intent = new Intent(getActivity(),PassagerAuthor.class);
				 break;
			 case 3:
				intent = new Intent(getActivity(),AddDriverCommonRoutes.class);
				 break;
			 case 4:
				 intent = new Intent(getActivity(),ToBeOwerUploadDriver.class);
				 break;
			 case 5:
				intent = new Intent(getActivity(),ToBeOwerUploadDrivring.class);
				 break;
			 case 6:
				intent = new Intent(getActivity(),LoginFromOldDriver.class);
				 break;
			 }
			 if(intent != null)	 intent.putExtra(IntentExtra.EXTRA_IS_AUTHOR_REFUSE,true);
			 startActivity(intent);
		}
	}
下略....


网页端负责

<html>
<head>
<script type="text/javascript">
function responseApp(var result){
	 document.getElementById("content").innerHTML = result;
	alert("dialog");
}

</script>
</head>
<body>
<span id="content"></span>
<a onClick="window.intbird.login_register()" href="";></a><p/>
<a onClick="window.intbird.author_drirver()" href="";></a><p/>
<a onClick="window.intbird.author_passager()" href="";></a><p/>
<a onClick="window.intbird.order_details_passager(120)" href="";></a><p/>
<a onClick="window.intbird.order_details_driver(120)" href="";></a><p/>
<a onClick="window.intbird.route_details(120)" href="";></a><p/>
<a onClick="window.intbird.carneeds_publish()" href="";></a><p/>
<a onClick="window.intbird.carneeds_list_main()" href="";></a><p/>
<a onClick="window.intbird.carneeds_list_nearby()" href="";></a><p/>
</body>
</html>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值