SecondActivity.java
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package xdycom.abao.myapplication;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import xdycom.abao.myapplication.application.MyApplicaiton;
import xdycom.abao.myapplication.utisl.StringUtils;
import xdycom.abao.myapplication.view.WebViewBase;
public class SecondActivity extends AppCompatActivity {
WebView webViewBase;
private final static String URL="http://h5.test.3mang.com/web/";
private Button bt_class;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
bt_class= (Button) this.findViewById(R.id.bt_class);
bt_class.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id="init";
String jsonParmps="{\n" +
" \"classId\": 527720984,\n" +
" \"portal\": \"112.126.80.182:90\",\n" +
" \"userRole\": \"normal\",\n" +
" \"userName\": \"\",\n" +
" \"userId\": 0\n" +
"}";
String jsonParmp= StringUtils.stringToJson(jsonParmps,true);
Log.e("转化后的json:",""+jsonParmp);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webViewBase.evaluateJavascript("javascript:_native2js('" + id + "','"+jsonParmp+"')", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
Toast.makeText(SecondActivity.this, "回调调用js" +
"" + value, Toast.LENGTH_SHORT).show();
}
});
}else{
webViewBase.loadUrl("javascript:imageVal('init')");
}
}
});
init();
}
public void init(){
webViewBase=new WebView(MyApplicaiton.getInstance());
WebSettings webSettings=webViewBase.getSettings();
webSettings.setJavaScriptEnabled(true);
// webViewBase.setWebViewClient(new WebViewClient(){
// @Override
// public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// return super.shouldOverrideUrlLoading(view, request);
// }
// });
webViewBase.addJavascriptInterface(new MyJs(),"xdyAndroid");
webViewBase.loadUrl(URL);
}
class MyJs {
@JavascriptInterface
public String _js2native(String id,String parmp){
/* _native2js("接口名称","参数为JOSN字符串")*/
Log.e("s3sjs2native","id:"+id+" parmp:"+parmp);
return parmp;
}
@JavascriptInterface
public String _js2native(String parmp){
/* _native2js("接口名称","参数为JOSN字符串")*/
Log.e("js2native", " parmp:"+parmp);
return parmp;
}
}
}