2019-11-18 Android调用JS(开发前端)JS调用Android

JS调用Android原图


8339984-684b7e9b507676c5.webp (1).jpg

Android调用JS原图


8339984-2a47c8a27921298a.webp.jpg

网络权限
<uses-permission android:name="android.permission.INTERNET"/>

activity_main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
<WebView
    android:id="@+id/web"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"/>

<Button
    android:onClick="call"
    android:text="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

HTML(new.html)文件

<!DOCTYPE html>
<html>
<head>
           <title>dag</title>
        <style type="text/css">
                   h1{
                     color:red;
}
</style>
</head>
<body>
         <h1>hello</h1>
<button onClick="call()">but</button>
<script>
function fun1(msg){
 document.querySelector("h1").innerHTM=msg
return"hello:"+msg
}
function call(){
MyService jump();
}
</script>
</body>
</html>

MyService.java文件

package com.example.fu1118;

import android.content.Context;
import android.content.Intent;
import android.webkit.JavascriptInterface;

public class MyService {
private Context context;
 public MyService(Context context) {
        this.context = context;
    }
    @JavascriptInterface
public String word(String s){
return "hello"+s;
}
public void jump(){
Intent intent=new Intent();
intent.setClass(context,OtherActivity.class);
context.startActivity(intent);
}
}

mainActivity主文件

package com.example.fu1118;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.web);
    WebSettrings websettrings=WebString.getSettring();
   websettrings.setCacheMode(WebSettings.LOAD_NO_CACHE); //关闭webview中缓存
        webSettings.setJavaScriptEnabled(true);//设置自适应屏幕,两者合用
        webView.loadUrl("file:///android_asset/new.html");
       webView.addJavascriptInterface(new MyService(this),"MyService");
}
public void call(View view){
webView.evaluateJavascript("javascript:show('world')",new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
Toast.makeText(MainActivity.this, value, Toast.LENGTH_LONG).show();
            }
});
}
}


新建HTML的方法
new->Folder->Assets Folder
assets->file->new.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。