当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现阅读进度记忆功能

Android实现阅读进度记忆功能

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了android控件webview实现保存阅读进度的具体代码,供大家参考,具体内容如下

用户提了一个要求,要求保存他的阅读进度,然后在他下次阅读的时候可以继续阅读,然后动手实现了一下,是这样的。

我用的控件是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();
    }
  }

这中间,link是我的加载网址

@override
  public void onpause() {
    super.onpause();

    if (webview != null) {
      int scrolly = webview.getscrolly();
      cacheutils.putint(this, link, scrolly);//保存访问的位置
    }
  }

最后贴出工具类

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);
  }

}

三步就完成了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网