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"/>
相關文章
- Android Webview H5 秒開方案實現AndroidWebViewH5
- WebView秒開方案探索WebView
- Android WebView Resources$NotFoundExceptionAndroidWebViewException
- Android WebView的坑AndroidWebView
- android webview總結AndroidWebView
- Android webview JS 互動AndroidWebViewJS
- 再學Android之WebViewAndroidWebView
- Android App秒開的奧祕AndroidAPP
- Android Webview和ScrollView衝突和WebView使用總結AndroidWebView
- Android webview圖片過大AndroidWebView
- Flutter 與 Android 原生 WebView 對比FlutterAndroidWebView
- Android 和 Webview 如何相互 sayHello(一)AndroidWebView
- Android webview 與 js(Vue) 互動AndroidWebViewJSVue
- Android與WebView資料互動AndroidWebView
- Android中WebView的使用指南:AndroidWebView
- Android 中 WebView 的除錯方法AndroidWebView除錯
- 你不知道的Android WebView漏洞AndroidWebView
- Appium Android 獲取WebView元素的方法APPAndroidWebView
- Android webview “location.replace” 不起作用AndroidWebView
- Android 必知必會 – Android Splash 頁秒開之細節處理Android
- Android 面試開源框架篇Android面試框架
- Android中使用AndroidTagGroup開源框架Android框架
- 【SJSB】android-javascript開發框架JSAndroidJavaScript框架
- Android WebView 中的 Html 網頁定位操作AndroidWebViewHTML網頁
- Android效能優化(1)—webview優化篇Android優化WebView
- [Android開源框架]RxHttp使用說明Android框架HTTP
- Android開發:系統程式中使用Webview引發異常的處理AndroidWebView
- Android WebView安全方面的一些坑AndroidWebView
- Android中使用WebView遇到的問題總結:AndroidWebView
- Android 8.0上webView載入url,不回撥onPageFinishedAndroidWebView
- 開源Android容器化框架Atlas開發者指南Android框架
- Android開源框架原始碼鑑賞:VirtualAPKAndroid框架原始碼APK
- Android開源框架如何說-Gilde4Android框架
- Android開發框架Collection-kotlin(更新篇)Android框架Kotlin
- Android模組開發框架 LiveData+ViewModelAndroid框架LiveDataView
- Android開源框架原始碼鑑賞:EventBusAndroid框架原始碼
- Android之五大應用開發框架Android框架
- Android T-MVVM深度解耦開發框架AndroidMVVM解耦框架