七、Android和H5的交互

文件目录


1.png

一、Android与H5的交互

JavaAndJavaScriptCall.html



    
    







这是H5页面

AndroidAndJsCallActivity.java

public class AndroidAndJsCallActivity extends Activity implements View.OnClickListener {

    private EditText etNumber;
    private EditText etPassword;
    private Button btnLogin;
    private WebView webView;

    /**
     * Find the Views in the layout
*
* Auto-created on 2019-02-08 15:24:01 by Android Layout Finder * (http://www.buzzingandroid.com/tools/android-layout-finder) */ private void findViews() { setContentView(R.layout.activity_android_and_js_call); etNumber = (EditText) findViewById(R.id.et_number); etPassword = (EditText) findViewById(R.id.et_password); btnLogin = (Button) findViewById(R.id.btn_login); btnLogin.setOnClickListener(this); } /** * Handle button click events
*
* Auto-created on 2019-02-08 15:24:01 by Android Layout Finder * (http://www.buzzingandroid.com/tools/android-layout-finder) */ @Override public void onClick(View v) { if (v == btnLogin) { // Handle clicks for btnLogin login(); } } private void login() { String account = etNumber.getText().toString().trim(); String password = etPassword.getText().toString().trim(); if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) { Toast.makeText(this, "账号或密码不能为空", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "登录", Toast.LENGTH_SHORT).show(); login(account);//加载h5页面 } } private void login(String account) { //java调用h5 webView.loadUrl("javascript:javaCallJs(" + "'" + account + "'" + ")"); setContentView(webView); } private void initWebview() { webView = new WebView(this); WebSettings settings = webView.getSettings(); //设置支持js settings.setJavaScriptEnabled(true); //不调用系统浏览器 webView.setWebViewClient(new WebViewClient()); //webView.loadUrl("https://www.baidu.com/"); //加载本地js webView.loadUrl("file:///android_asset/JavaAndJavaScriptCall.html"); //设置支持js调用java webView.addJavascriptInterface(new JavaScriptInterface(), "Android"); } class JavaScriptInterface { @JavascriptInterface public void showToast() { Toast.makeText(AndroidAndJsCallActivity.this, "我是Java代码", Toast.LENGTH_SHORT).show(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); findViews(); initWebview(); } }

activity_android_and_js_call.xml




    

        

    

    

        

            

            

            

            

            

在H5页面调用Android播放视频

链接:https://pan.baidu.com/s/1sHQCTOSzKacMNRtNCJXXLA
提取码:5c49
html所需要的资源文件
H5CallJavaVedioActivity .java

public class H5CallJavaVedioActivity extends Activity {

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5_call_java_vedio);

        webview = (WebView) findViewById(R.id.webview);

        initWebview();


    }

    private void initWebview() {
        WebSettings settings = webview.getSettings();
        //设置支持js
        settings.setJavaScriptEnabled(true);

        //不调用浏览器
        webview.setWebViewClient(new WebViewClient());

        //webView.loadUrl("https://www.baidu.com/");
        //加载本地js
        webview.loadUrl("file:///android_asset/RealNetJSCallJavaActivity.htm");

        //设置支持js调用java
        webview.addJavascriptInterface(new JavaScriptInterface(), "android");

    }

    class JavaScriptInterface {
        @JavascriptInterface
        public void playVideo(String id, String url, String title) {
            //调起系统所有播放器
            Intent intent = new Intent();
            intent.setDataAndType(Uri.parse(url), "video/*");
            startActivity(intent);

        }

    }
}

activity_h5_call_java_vedio.xml




    



你可能感兴趣的:(七、Android和H5的交互)