public class LuckyDrawClickActivity extends BaseActivity<LuckyDrawClickPresenter> implements LuckyDrawClickContract.View {
private WebView webView;
private String id;
@Override
public void setDaggerComponent(@NonNull AppComponent appComponent) {
DaggerLuckyDrawClickComponent // 如找不到该类,请编译一下项目
.builder()
.appComponent(appComponent)
.luckyDrawClickModule(new LuckyDrawClickModule(this))
.build()
.inject(this);
}
public static void launchActivity(Activity activity, String id) {
Intent intent = new Intent(activity, LuckyDrawClickActivity.class);
intent.putExtra("id", id);
activity.startActivity(intent);
}
@Override
public int initLayout(@Nullable Bundle savedInstanceState) {
return R.layout.activity_lucky_draw_click;
}
@Override
public void init(@Nullable Bundle savedInstanceState) {
webView = (WebView) findViewById(R.id.wv_webview);
id = getIntent().getStringExtra("id");
loadWeb();
}
// @SuppressLint("JavascriptInterface")
// public void loadWeb() {
// //String url = "https://www.baidu.com/";
// // String url = "file:///android_asset/index.html";
// String url = "http://192.168.2.92:8081/index.html";
// //此方法可以在webview中打开链接而不会跳转到外部浏览器
// webView.getSettings().setJavaScriptEnabled(true);
// webView.loadUrl(url);
//
// webView.setWebViewClient(new WebViewClient() {
// @Override
// public void onPageFinished(WebView view, String url) {
// super.onPageFinished(view, url);
// // view.loadUrl("javascript:getDrawsRemainder(\"" + "1" + "\")");
// view.loadUrl("javascript:get()");
// }
// });
// }
@SuppressLint("JavascriptInterface")
public void loadWeb() {
String url = "http://192.168.2.92:8081/index.html";
//此方法可以在webview中打开链接而不会跳转到外部浏览器
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(url);
//支持弹窗,也就是支持html网页弹框
webView.setWebChromeClient(new WebChromeClient() {
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// view.loadUrl("javascript:getDrawsRemainder(\"" + "1" + "\")");
view.loadUrl("javascript:getInit(" + id + ")");
}
});
//支持html中javascript解析,不管是不是js和android交互,只要网页中含有js都要
webView.getSettings().setJavaScriptEnabled(true);
}
//重载onKeyDown的函数,使其在页面内回退,而不是直接退出程序
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* 默认请求网络数据方法
*/
@Override
public void onRequest() {
}
/**
* p层返回数据
*/
@Override
public void showSuccessData(LuckyDrawClickResponse response) {
}
}
android与H5