webview定义
WebView webView = (WebView) findViewById(R.id.login_webview);
webView.getSettings().setJavaScriptEnabled(true);
//前端调用android接口
webView.addJavascriptInterface(new JsforAndroid(), "vimmoneAndroid");
webView.setWebViewClient(new WebViewClient() {
//当加载页面完成的时候回调
@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, "正在准备初始化,请等待..." , 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的页面中js使用以下调用:
window.vimmoneAndroid.userSignIn(
this.ruleForm2.number,this.ruleForm2.pass);
网友评论