crosswalk---android和JS互相調用

1.javascript

JS調用android
對象映射 

Java映射代碼html

public class MainActivity extends Activity{
    XWalkView xWalkView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        XWalkPreferences.setValue("enable-javascript", true);

        xWalkView = findViewById(R.id.web);
        xWalkView.addJavascriptInterface(this, "android");
        xWalkView.loadUrl("file:///android_asset/index.html");
    }
	
	@org.xwalk.core.JavascriptInterface
    public void test(String JSResult) {
        Log.e("JavascriptHandler", "html content:" + JSResult);
    }
}

JS中能夠直接使用java

android.test("這是在JS中調用函數");
回調攔截 url   
回調攔截JS對話框

1. JS中執行代碼android

promptResult = prompt("js://serverAddr?currServerAddr=調用clickprompt&arg2=222", "這是傳入android的默認參數");

2. android中的處理過程web

  如: http://www.javashuo.com/article/p-etzytcrc-dc.html 中的prompt監聽ide

注:使用中android中未執行 result.confirmWithResult(); 時,JS中的定時器將中止工做。函數

 android中this

 執行JSlua

 
loadUrl()
xWalkView.loadUrl("javascript:document.getElementsByTagName(\"body\")[0].innerHTML=\"android中改變了H5\"");

  直接在android中執行JS代碼後,頁面僅僅顯示 「android中改變了H5」url

evaluateJavascript()
JS代碼
function testCalls()
{
	return "JS中返回的字符串"
}

 android代碼

xWalkView.evaluateJavascript("javascript:testCalls();", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Log.e("evaluateJavascript", s);
    }
});

 查看結果:logcat -c && logcat  *:E

相關文章
相關標籤/搜索