webview定義
WebView webView = (WebView) findViewById(R.id.login_webview);
webView.getSettings().setJavaScriptEnabled(true);
//前端調(diào)用android接口
webView.addJavascriptInterface(new JsforAndroid(), "vimmoneAndroid");
webView.setWebViewClient(new WebViewClient() {
//當(dāng)加載頁(yè)面完成的時(shí)候回調(diào)
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
webView.loadUrl(Constants.VIEW_HOST+"/signByPhone/inByMyApp");
自定義接口
private class JsforAndroid {
@JavascriptInterface
public void userSignIn(final String userId, final String password){
//提供給前端的接口
Toast.makeText(LoginActivity.this, "正在準(zhǔn)備初始化,請(qǐng)等待..." , Toast.LENGTH_SHORT).show();
SharedPreferences.Editor editor = getSharedPreferences(Constants.SHARE_NAME, MODE_PRIVATE).edit();
editor.putString(Constants.SHARE_USER,userId);
editor.putString(Constants.SHARE_PASSWORD,password);
editor.commit();
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
}
- 在webview load的頁(yè)面中js使用以下調(diào)用:
window.vimmoneAndroid.userSignIn(
this.ruleForm2.number,this.ruleForm2.pass);