LunchActivity.java
6.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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.LayoutInflater;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
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.utisl.XdyWebView;
public class LunchActivity extends AppCompatActivity implements View.OnClickListener{
private Button bt_start;
private MyWebView myWebView;
WebView webView;
private final static String URL="http://h5.test.3mang.com/web/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lunch);
bt_start= (Button) this.findViewById(R.id.bt_start);
bt_start.setOnClickListener(this);
init();
// initData();
}
public void init (){
bt_start= (Button) this.findViewById(R.id.bt_start);
bt_start.setOnClickListener(this);
// myWebView= (MyWebView) this.findViewById(R.id.mywebview);
}
public void initData(){
myWebView=new MyWebView(this);
myWebView.setWebChromeClient(new WebChromeClient(){
});
myWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
myWebView.loadUrl(URL);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.e("webview","errorCode"+errorCode);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_start:
// initClass();
// initClass();
inidwebView();
break;
}
}
/**
* 初始化课堂
*/
public void initClass(){
View view= LayoutInflater.from(this).inflate(R.layout.activity_main,null);
webView= (WebView) view.findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
}
});
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
super.onConsoleMessage(message, lineNumber, sourceID);
Log.e(null,"Message:"+message+" lineNumber:");
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJs(),"xdyAndroid" );
webView.loadUrl(URL);
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);
_native2js(id,jsonParmp);
}
class MyJs {
@JavascriptInterface
public String _js2native(String id,String parmp){
/* _native2js("接口名称","参数为JOSN字符串")*/
Log.e("js2native","id:"+id+" parmp:"+parmp);
return parmp;
}
@JavascriptInterface
public String _js2native(String parmp){
/* _native2js("接口名称","参数为JOSN字符串")*/
Log.e("js2native", " parmp:"+parmp);
return parmp;
}
}
public String _native2js( String id, String parmp) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("javascript:_native2js('" + id + "','" + parmp + "')", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
Log.e("显示:界面","啊哈");
}
});
} else {
// mWebView.loadUrl("javascript:imageVal('init')");
}
}
return "";
}
public void inidwebView(){
WebView xdyWebView=new WebView(this);
xdyWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
});
xdyWebView.getSettings().setJavaScriptEnabled(true);//支持Html5标签
xdyWebView.getSettings().setDomStorageEnabled(true);//支持Html5标签
xdyWebView.addJavascriptInterface(new MyJs(),"xdyAndroid");
xdyWebView.loadUrl(URL);
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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
xdyWebView.evaluateJavascript("javascript:_native2js('" + id + "','" + jsonParmp + "')", new ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
Log.e("显示:界面","啊哈");
}
});
} else {
xdyWebView.loadUrl("javascript:imageVal('init')");
}
}
}