大概問題來源:
程序需要webview加載h5界面,但有一個H5界面非常怪,進入界面什麼問題也沒有,從桌面回來,分享回來也沒有問題,但中跳轉Activity或者關閉當前webivew的Activity時,就閃退重啓了!!!
log:
E/ANDR-PERF-OPTSHANDLER: perf_lock_rel: updated /sys/class/scsi_host/host0/../../../clkscale_enable with 1
return value 2
E/cnss-daemon: Stale or unreachable neighbors, ndm state: 4
E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=421, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=422, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
E/cnss-daemon: Stale or unreachable neighbors, ndm state: 4
E/InputDispatcher: channel '694a7a5 com.cyy.demo/com.cyy.demo.client.main.activity.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
E/InputDispatcher: channel '4ccdb88 com.cyy.demo/com.cyy.demo.client.main.activity.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
E/InputDispatcher: channel 'a704bbf com.cyy.demo/com.cyy.demo.client.quickorder.activity.QuickHandleOrderActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
E/InputDispatcher: channel '25d6073 com.cyy.demo/com.cyy.demo.framework.base.CommWebActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
E/ANDR-PERF-OPTSHANDLER: perf_lock_rel: updated /sys/class/scsi_host/host0/../../../clkscale_enable with 1
return value 2
然後就考慮換第三方webview了,最終選擇了TBS(騰訊瀏覽服務)
sdk下載:https://x5.tencent.com/tbs/sdk.html#選擇他是因爲是中文,不用在github找到還要翻譯,其二是因爲有jar接入,方便:
最後上代碼:
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;
public class CommWebActivity extends BaseActivity {
private WebView mWebView;
public static final String KEY_URL = "key_url";
public static final String KEY_TITLE = "key_title";
public static void openWeb(Context c, String url) {
openWeb(c, url, null);
}
/**
*
* @param c
* Context
* @param url
* url
* @param title
* 顯示title,當title爲""時,自動獲取url的title
*/
public static void openWeb(Context c, String url, String title) {
if (StringUtil.isEmpty(url)) {
return;
}
Intent intent = new Intent(c, CommWebActivity.class);
intent.putExtra(KEY_URL, url);
intent.putExtra(KEY_TITLE, title);
c.startActivity(intent);
}
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_web);
setLeftText("返回");
initView();
initData();
}
public void initView() {
mWebView = (WebView) findViewById(R.id.webview);
}
@SuppressLint("SetJavaScriptEnabled")
public void initData() {
String url = getIntent().getStringExtra(KEY_URL);
if (StringUtil.isEmpty(url)) {
finish();
return;
}
String title = getIntent().getStringExtra(KEY_TITLE);
if (null == title) {
hideTitle(true);
} else {
setTitleText(title);
}
com.tencent.smtt.sdk.WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.setWebChromeClient(new WebChromeClient() {
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder builder = new AlertDialog.Builder(CommWebActivity.this);
builder.setCancelable(false).setTitle("溫馨提示").setMessage(message)
.setNeutralButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
}).show();
result.cancel();
return true;
}
});
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// showLoadDialog();
showProgressDialog();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dismissProgressDialog();
if (TextUtils.isEmpty(getTitleText())) {
setTitleText(mWebView.getTitle());
}
}
});
mWebView.loadUrl(url);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
mWebView.destroy();
}
}
}
最後在Application要加上:
super.onCreate();
//蒐集本地tbs內核信息並上報服務器,服務器返回結果決定使用哪個內核。
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onViewInitFinished(boolean arg0) {
// TODO Auto-generated method stub
//x5內核初始化完成的回調,爲true表示x5內核加載成功,否則表示x5內核加載失敗,會自動切換到系統內核。
Log.d("app", " onViewInitFinished is " + arg0);
}
@Override
public void onCoreInitFinished() {
// TODO Auto-generated method stub
}
};
//x5內核初始化接口
QbSdk.initX5Environment(getApplicationContext(), cb);