Realization of reading progress memory function

The user made a request to save his reading progress, and then he can continue to read the next time he reads, and then implemented it by hand, it is like this.
The control I use is WebView

public class WebViewClientEmb extends WebViewClient {

        // 在WebView中而不是系统默认浏览器中显示页面
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            System.out.println("Url---------->"+url);
            return true;
        }

        // 页面载入前调用
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }

        // 页面载入完成后调用
        @Override
        public void onPageFinished(WebView webView, String url) {

            int position = CacheUtils.getInt(Laws_DetailActivity.this,link, 0);
            webView.scrollTo(0, position);//webview加载完成后直接定位到上次访问的位置
            mLoadingDialog.dismiss();
        }
    }

In the middle, link is my loading URL

 @Override
    public void onPause() {
        super.onPause();

       if (webView != null) {
            int scrollY = webView.getScrollY();
            CacheUtils.putInt(this, link, scrollY);//保存访问的位置
        }
    }

Finally posted the tool class

public class CacheUtils {
    private static final String NAME = "";
    private static SharedPreferences sp = null;


    // 存Strings
    public static void putString(Context context, String key, String value) {
        if (sp == null) {
            sp = context.getSharedPreferences(NAME,
                    Context.MODE_PRIVATE);
        }
        sp.edit().putString(key, value).commit();
    }


    // 取String
    public static String getString(Context context, String key, String defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(NAME,
                    Context.MODE_PRIVATE);
        }
        return sp.getString(key, defValue);
    }


    //存Int值
    public static void putInt(Context context, String key, int value) {
        if (sp == null) {
            sp = context.getSharedPreferences(NAME,
                    Context.MODE_PRIVATE);
        }
        sp.edit().putInt(key, value).commit();
    }

    //取int值
    public static int getInt(Context context, String key, int defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(NAME,
                    Context.MODE_PRIVATE);
        }
        return sp.getInt(key, defValue);
    }

}

Three steps are done

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325624050&siteId=291194637