android、IOSはwebviewとHTMLによる統合
2850 ワード
知識点:
1)android html
2)html javascript android IOS
3)IOS android ( IOS oc , 。。。。 IOS ,html )
htmlコード
3000.00
5000.00
var ua = navigator.userAgent.toLowerCase();
var webType;
if (/iphone|ipad|ipod/.test(ua)){
webViewType = 'iphone';
} else if (/android/.test(ua)){
webViewType = 'android';
}else{
webViewType='pc';
}
<!-- orderXml -->
function sendOrderXml(orderXml){
if('iphone'==webViewType){
var f = document.forms[0];
f.action="ios://orderxml="+encodeURI(orderXml);
f.submit();
}else if('android'==webViewType){
window.myObj.dopay(encodeURI(orderXml));
}else if('pc' == webViewType){
window.myObj.dopay(encodeURI(orderXml));
}
}
Android コードpackage com.htjc.baseframe;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
public class WebViewActivityextends BaseActivtiy{
private WebView webview;
private LinearLayout ll_rootView;
@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface" })
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
ll_rootView = new LinearLayout(this);
ll_rootView.setOrientation(LinearLayout.VERTICAL);
webview = new WebView(this);
WebSettings settings =webview.getSettings();
settings.setJavaScriptEnabled(true);
// webview.loadUrl("http://192.168.1.117:8080/webview/test.html");
webview.loadUrl("file:///android_asset/test.html");
webview.addJavascriptInterface(new JavaScriptObject(),"myObj");
ll_rootView.addView(webview);
setContentView(ll_rootView);
}
public class JavaScriptObject {
@JavascriptInterface
/**
*
* @param name
*/
public void dopay(Stringname){
Log.i("xiexie",name+"----");
String result="";
try {
result = URLDecoder.decode(name,"UTF-8");
} catch (UnsupportedEncodingExceptione) {
e.printStackTrace();
}
System.out.println(result);
Log.i("xiexie11",result+"----");
}
}
}