package com.yeshi.base.ui;
|
|
import android.content.Intent;
|
import android.os.Bundle;
|
import android.view.KeyEvent;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.ViewGroup;
|
import android.widget.FrameLayout;
|
import android.widget.ProgressBar;
|
import android.widget.TextView;
|
|
import com.alibaba.android.arouter.facade.annotation.Route;
|
import com.alibaba.android.arouter.launcher.ARouter;
|
import com.tencent.smtt.sdk.DownloadListener;
|
import com.tencent.smtt.sdk.WebChromeClient;
|
import com.tencent.smtt.sdk.WebSettings;
|
import com.tencent.smtt.sdk.WebView;
|
import com.tencent.smtt.sdk.WebViewClient;
|
import com.umeng.analytics.MobclickAgent;
|
import com.yeshi.base.utils.MGJavaInterface;
|
import com.yeshi.base.utils.RouteConstant;
|
import com.yeshi.base.utils.x5.X5WebView;
|
import com.yeshi.base.R;
|
|
@Route(path = RouteConstant.PATH_WEB_BROWSER)
|
public class BrowserActivity extends BaseActivity implements OnClickListener {
|
|
private TextView tv_top_bar_left;
|
private TextView tv_top_bar_left2;
|
private TextView tv_top_bar_middle;
|
private X5WebView webview;
|
private FrameLayout fl_webview;
|
ProgressBar progressBar;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.browser_activity);
|
ARouter.getInstance().inject(this);
|
tv_top_bar_left = (TextView) findViewById(R.id.tv_top_bar_left);
|
tv_top_bar_left2 = (TextView) findViewById(R.id.tv_top_bar_left2);
|
tv_top_bar_left2.setVisibility(View.VISIBLE);
|
tv_top_bar_middle = (TextView) findViewById(R.id.tv_top_bar_middle);
|
fl_webview = (FrameLayout) findViewById(R.id.fl_webview);
|
tv_top_bar_left2.setText("关闭");
|
tv_top_bar_left.setOnClickListener(this);
|
tv_top_bar_left2.setOnClickListener(this);
|
|
webview = new X5WebView(BrowserActivity.this);
|
webview.setDownloadListener(new DownloadListener() {
|
|
@Override
|
public void onDownloadStart(String arg0, String arg1, String arg2,
|
String arg3, long arg4) {
|
}
|
|
});
|
fl_webview.addView(webview, new FrameLayout.LayoutParams(
|
FrameLayout.LayoutParams.MATCH_PARENT,
|
FrameLayout.LayoutParams.MATCH_PARENT));
|
WebSettings webSettings = webview.getSettings();
|
webSettings.setJavaScriptEnabled(true);
|
webSettings.setDomStorageEnabled(true);
|
webview.addJavascriptInterface(new MGJavaInterface(BrowserActivity.this, webview), "yestv");
|
webview.setDrawingCacheEnabled(true);
|
webview.setWebViewClient(new WebViewClient() {
|
@Override
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
if (url != null && url.startsWith("buwanprotocol://")) {
|
String murl = url.split("buwanprotocol://")[1];
|
String[] params = murl.split("#");
|
if (params[0].equalsIgnoreCase("playvideo")) {
|
Bundle bundle = new Bundle();
|
bundle.putString("Id", params[1]);
|
bundle.putString("ResourceId", params[2]);
|
bundle.putString("DetailId", params[3]);
|
bundle.putString("Share", "0");
|
bundle.putString("ThirdType", "0");
|
ARouter.getInstance().build(RouteConstant.PATH_VIDEO_DETAIL)
|
.withBundle(null, bundle)
|
.navigation();
|
}
|
return true;
|
} else if ((url != null && (url.contains("/tbopen/")) || (!url.startsWith("http")))) {
|
return true;
|
}
|
return super.shouldOverrideUrlLoading(webview, url);
|
}
|
|
@Override
|
public void onPageFinished(WebView webView, String s) {
|
super.onPageFinished(webView, s);
|
}
|
});
|
progressBar = findViewById(R.id.myProgressBar);
|
progressBar.setMax(100);
|
progressBar.setProgressDrawable(this.getResources().getDrawable(
|
R.drawable.color_progressbar));
|
webview.setWebChromeClient(new WebChromeClient() {
|
|
@Override
|
public void onReceivedTitle(WebView view, String title) {
|
tv_top_bar_middle.setText(title);
|
}
|
|
@Override
|
public void onProgressChanged(WebView webView, int i) {
|
if (i == 100) {
|
progressBar.setVisibility(View.GONE);
|
} else {
|
if (View.INVISIBLE == progressBar.getVisibility()) {
|
progressBar.setVisibility(View.VISIBLE);
|
}
|
progressBar.setProgress(i);
|
}
|
super.onProgressChanged(webView, i);
|
}
|
});
|
webview.loadUrl(getIntent().getStringExtra("url"));
|
}
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
}
|
|
@Override
|
public void onPause() {
|
super.onPause();
|
}
|
|
@Override
|
protected void onDestroy() {
|
if (webview != null) {
|
webview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
|
webview.clearHistory();
|
|
((ViewGroup) webview.getParent()).removeView(webview);
|
webview.destroy();
|
webview = null;
|
}
|
super.onDestroy();
|
}
|
|
@Override
|
protected void onStop() {
|
super.onStop();
|
}
|
|
@Override
|
public void onClick(View v) {
|
int id = v.getId();
|
if (id == R.id.tv_top_bar_left) {
|
if (!webview.canGoBack())
|
finish();
|
else {
|
webview.goBack();
|
}
|
} else if (id == R.id.tv_top_bar_left2) {
|
finish();
|
}
|
}
|
|
// 设置回退
|
// 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
|
@Override
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
|
webview.goBack(); // goBack()表示返回WebView的上一页面
|
return true;
|
}
|
return super.onKeyDown(keyCode, event);
|
}
|
}
|