android webview秒開框架
github上找一個webview框架,發現這個VasSonic ,速度還是很快的.
下面說下使用步驟:
1.gradle整合
implementation "com.tencent.sonic:sdk:$sonic_version"//webview
2.HostSonicRuntime.java
public class HostSonicRuntime extends SonicRuntime{
public HostSonicRuntime(Context context) {
super(context);
}
@Override
public void log(String tag, int level, String message) {
}
@Override
public String getCookie(String url) {
return null;
}
@Override
public boolean setCookie(String url, List<String> cookies) {
return false;
}
@Override
public String getUserAgent() {
return "lwp";
}
@Override
public String getCurrentUserAccount() {
return "lwp";
}
@Override
public boolean isSonicUrl(String url) {
return false;
}
@Override
public Object createWebResourceResponse(String mimeType, String encoding, InputStream data, Map<String, String> headers) {
return null;
}
@Override
public boolean isNetworkValid() {
return false;
}
@Override
public void showToast(CharSequence text, int duration) {
}
@Override
public void postTaskToThread(Runnable task, long delayMillis) {
}
@Override
public void notifyError(SonicSessionClient client, String url, int errorCode) {
}
/**
* @return the file path which is used to save Sonic caches.
*/
@Override
public File getSonicCacheDir() {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "sonic/";
File file = new File(path.trim());
if(!file.exists()){
file.mkdir();
}
return file;
}
}
3.SonicSessionClientImpl.java
*/
public class SonicSessionClientImpl extends SonicSessionClient {
private WebView webView;
public void bindWebView(WebView webView) {
this.webView = webView;
}
@Override
public void loadUrl(String url, Bundle extraData) {
webView.loadUrl(url);
}
@Override
public void loadDataWithBaseUrl(String baseUrl, String data, String mimeType, String encoding,
String historyUrl) {
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
}
@Override
public void loadDataWithBaseUrlAndHeader(String baseUrl, String data, String mimeType, String encoding, String historyUrl, HashMap<String, String> headers) {
webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
}
public WebView getWebView() {
return webView;
}
}
4.SonicJavaScriptInterface,這個在官方給的demo中謝了,但是教程沒寫
public class SonicJavaScriptInterface {
private final SonicSessionClientImpl sessionClient;
private final Intent intent;
public static final String PARAM_CLICK_TIME = "clickTime";
public static final String PARAM_LOAD_URL_TIME = "loadUrlTime";
public SonicJavaScriptInterface(SonicSessionClientImpl sessionClient, Intent intent) {
this.sessionClient = sessionClient;
this.intent = intent;
}
@JavascriptInterface
public void getDiffData() {
// the callback function of demo page is hardcode as 'getDiffDataCallback'
getDiffData2("getDiffDataCallback");
}
@JavascriptInterface
public void getDiffData2(final String jsCallbackFunc) {
if (null != sessionClient) {
sessionClient.getDiffData(new SonicDiffDataCallback() {
@Override
public void callback(final String resultData) {
Runnable callbackRunnable = new Runnable() {
@Override
public void run() {
String jsCode = "javascript:" + jsCallbackFunc + "('"+ toJsString(resultData) + "')";
sessionClient.getWebView().loadUrl(jsCode);
}
};
if (Looper.getMainLooper() == Looper.myLooper()) {
callbackRunnable.run();
} else {
new Handler(Looper.getMainLooper()).post(callbackRunnable);
}
}
});
}
}
@JavascriptInterface
public String getPerformance() {
long clickTime = intent.getLongExtra(PARAM_CLICK_TIME, -1);
long loadUrlTime = intent.getLongExtra(PARAM_LOAD_URL_TIME, -1);
try {
JSONObject result = new JSONObject();
result.put(PARAM_CLICK_TIME, clickTime);
result.put(PARAM_LOAD_URL_TIME, loadUrlTime);
return result.toString();
} catch (Exception e) {
}
return "";
}
/*
* * From RFC 4627, "All Unicode characters may be placed within the quotation marks except
* for the characters that must be escaped: quotation mark,
* reverse solidus, and the control characters (U+0000 through U+001F)."
*/
private static String toJsString(String value) {
if (value == null) {
return "null";
}
StringBuilder out = new StringBuilder(1024);
for (int i = 0, length = value.length(); i < length; i++) {
char c = value.charAt(i);
switch (c) {
case '"':
case '\\':
case '/':
out.append('\\').append(c);
break;
case '\t':
out.append("\\t");
break;
case '\b':
out.append("\\b");
break;
case '\n':
out.append("\\n");
break;
case '\r':
out.append("\\r");
break;
case '\f':
out.append("\\f");
break;
default:
if (c <= 0x1F) {
out.append(String.format("\\u%04x", (int) c));
} else {
out.append(c);
}
break;
}
}
return out.toString();
}
}
5.在webview中使用,這個佈局檔案就是一個webview而已,就不貼了
public class WebViewActivity extends BaseActivity {
private String url;
private SonicSession sonicSession;
private Intent intent;
private WebView webView;
@Override
protected void initPresenter() {
}
@Override
protected void initData() {
}
@Override
protected void initView() {
intent = getIntent();
url = intent.getStringExtra(Constant.URL);
if (TextUtils.isEmpty(url)){
return;
}
// step 1: Initialize sonic engine if necessary, or maybe u can do this when application created
if (!SonicEngine.isGetInstanceAllowed()) {
SonicEngine.createInstance(new HostSonicRuntime(getApplication()), new SonicConfig.Builder().build());
}
SonicSessionClientImpl sonicSessionClient = null;
// step 2: Create SonicSession
sonicSession = SonicEngine.getInstance().createSession(url, new SonicSessionConfig.Builder().build());
if (null != sonicSession) {
sonicSession.bindClient(sonicSessionClient = new SonicSessionClientImpl());
} else {
// this only happen when a same sonic session is already running,
// u can comment following codes to feedback as a default mode.
// throw new UnknownError("create session fail!");
}
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (sonicSession != null) {
sonicSession.getSessionClient().pageFinish(url);
}
}
@TargetApi(21)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return shouldInterceptRequest(view, request.getUrl().toString());
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (sonicSession != null) {
//step 6: Call sessionClient.requestResource when host allow the application
// to return the local data .
return (WebResourceResponse) sonicSession.getSessionClient().requestResource(url);
}
return null;
}
});
WebSettings webSettings = webView.getSettings();
// step 4: bind javascript
// note:if api level lower than 17(android 4.2), addJavascriptInterface has security
// issue, please use x5 or see https://developer.android.com/reference/android/webkit/
// WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
webSettings.setJavaScriptEnabled(true);
webView.removeJavascriptInterface("searchBoxJavaBridge_");
intent.putExtra(SonicJavaScriptInterface.PARAM_LOAD_URL_TIME, System.currentTimeMillis());
webView.addJavascriptInterface(new SonicJavaScriptInterface(sonicSessionClient, intent), "sonic");
// init webview settings
webSettings.setAllowContentAccess(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
// step 5: webview is ready now, just tell session client to bind
if (sonicSessionClient != null) {
sonicSessionClient.bindWebView(webView);
sonicSessionClient.clientReady();
} else { // default mode
webView.loadUrl(url);
}
}
@Override
protected void initInjection() {
}
@Override
public int getLayoutId() {
return R.layout.activity_webview;
}
}
6.最後記得要加讀寫許可權
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
相關文章
- WebView秒開方案探索WebView
- Android Webview H5 秒開方案實現AndroidWebViewH5
- 使用localstorage和預載入做到webview秒開WebView
- Android混合開發之WebView使用總結AndroidWebView
- 開啟 Android WebView 的安全瀏覽模式AndroidWebView模式
- Android App秒開的奧祕AndroidAPP
- android webview總結AndroidWebView
- Android WebView Resources$NotFoundExceptionAndroidWebViewException
- Android WebView的坑AndroidWebView
- Android WebView 詳解AndroidWebView
- android webview 跳轉AndroidWebView
- Android混合開發之WebView與Javascript互動AndroidWebViewJavaScript
- Android WebView Memory Leak WebView記憶體洩漏AndroidWebView記憶體
- Android webview JS 互動AndroidWebViewJS
- 再學Android之WebViewAndroidWebView
- Android webview使用詳解AndroidWebView
- Android WebView 除錯方法AndroidWebView除錯
- Android WebView 優化之路AndroidWebView優化
- Android中WebView使用解析AndroidWebView
- android WebView總結(轉)AndroidWebView
- Android Webview 使用小結AndroidWebView
- Android 各個版本WebViewAndroidWebView
- Android WebView ScrollBar設定AndroidWebView
- Android 用WebView開發簡單的瀏覽器AndroidWebView瀏覽器
- android快速開發框架Android框架
- Android Webview和ScrollView衝突和WebView使用總結AndroidWebView
- Android開發小技巧之不再使用原生的WebView了AndroidWebView
- Android WebView載入TTF字型AndroidWebView
- Android layer type與WebView白屏AndroidWebView
- Android: HttpClient與Webview共享cookiesAndroidHTTPclientWebViewCookie
- android httpclient與webview cookie同步AndroidHTTPclientWebViewCookie
- Android webview圖片過大AndroidWebView
- Android端應用秒開優化體驗Android優化
- Android常用的開源框架Android框架
- Android 面試開源框架篇Android面試框架
- Android的MVC開源框架AndroidMVC框架
- iOS開發之WebViewiOSWebView
- Flutter 與 Android 原生 WebView 對比FlutterAndroidWebView