implementation 'com.just.agentweb:agentweb:4.1.4' // (必选)
implementation 'com.just.agentweb:filechooser:4.1.4'// (可选)
implementation 'com.download.library:Downloader:4.1.4'// (可选)
/**
* Created by cenxiaozhong on 2017/5/26.
* <p>
* source code https://github.com/Justson/AgentWeb
*/
public class WebPayActivity extends AppCompatActivity {
protected AgentWeb mAgentWeb;
private LinearLayout mLinearLayout;
@Override
protected int getLayoutId() {
return R.layout.activity_web_pay;
}
@Override
protected void initWidget() {
super.initWidget();
mLinearLayout = (LinearLayout) this.findViewById(R.id.container);
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent(mLinearLayout, new LinearLayout.LayoutParams(-1, -1))
.useDefaultIndicator()
.setWebChromeClient(mWebChromeClient)
.setWebViewClient(mWebViewClient)
.setMainFrameErrorView(R.layout.agentweb_error_page, -1)
.setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
.setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)//打开其他应用时,弹窗咨询用户是否前往其他应用
.interceptUnkownUrl() //拦截找不到相关页面的Scheme
.createAgentWeb()
.ready()
.go(getUrl());
mAgentWeb.getJsInterfaceHolder().addJavaObject("app", new JavaScriptinterface());
}
class JavaScriptinterface {
@JavascriptInterface
public void signComplete() {
mAgentWeb.getUrlLoader().loadUrl(Constants2.Host + "/ipage_success.html");
}
@JavascriptInterface
public void signError() {
mAgentWeb.getUrlLoader().loadUrl(Constants2.Host + "/ipage_error.html");
}
}
private WebViewClient mWebViewClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mAgentWeb.getJsAccessEntrace().quickCallJs("sign",
getIntent().getStringExtra("singInfo"),
Constants2.Host + "/ipage_success.html",
Constants2.Host + "/ipage_error.html ");
}
};
private WebChromeClient mWebChromeClient = new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
};
public String getUrl() {
return getIntent().getStringExtra("url");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mAgentWeb.handleKeyEvent(keyCode, event)) {
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onPause() {
mAgentWeb.getWebLifeCycle().onPause();
super.onPause();
}
@Override
protected void onResume() {
mAgentWeb.getWebLifeCycle().onResume();
super.onResume();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("Info", "onResult:" + requestCode + " onResult:" + resultCode);
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
AgentWebConfig.clearDiskCache(this);
mAgentWeb.getWebLifeCycle().onDestroy();
}
@Override
public void onBackPressed() {
if (mAgentWeb.getWebCreator().getWebView().canGoBack()) {
mAgentWeb.getWebCreator().getWebView().goBack();
} else {
finish();
}
}
public static void newIntent(BaseActivity mContext, String url, String singInfo) {
Intent intent = new Intent();
intent.setClass(mContext, WebPayActivity.class);
intent.putExtra("url", url);
intent.putExtra("singInfo", singInfo);
mContext.startActivity(intent);
}
}