android 带记忆功能的播放器源码,Android实现阅读进度记忆功能

本文实例为大家分享了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);

}

}

三步就完成了。

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值