首页 文章 精选 留言 我的

精选列表

搜索[android studio],共10000篇文章
优秀的个人博客,低调大师

4-SII--☆Android缓存文件(带有效时长)封装

零、前言 [1]把我的缓存文件工具改写成了策略模式,感觉还不错。 [2]以前静态方法调用,很方便,但看着就是不爽,代码真的太冗余了。 [3]突然灵光一闪,"少年,看你骨骼惊奇,策略模式了解一下吗。"便有此文。 [4]如果不了解SharedPreferences,可以先看这篇:1-SII--SharedPreferences完美封装 缓存策略类图 缓存策略.png 一、使用: //新建缓存对象 CacheWorker innerCache = new CacheWorker(new InnerFileStrategy(this)); //设置缓存 innerCache.setCache("toly", "hehe", 10); //获取缓存 String value = innerCache.getCache("toly"); //SD卡缓存 CacheWorker sdCache = new CacheWorker(new SDFileStrategy()); sdCache.setCache("toly", "hehe2", 10); String sdCach = sdCache.getCache("toly"); //SharedPreferences CacheWorker spCache = new CacheWorker(new SPStrategy(this)); spCache.setCache("toly1994.com", "{name:toly}", 10); String spValue = spCache.getCache("toly1994.com"); 缓存.png 二、附录:各类及实现 /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:20<br/> * 邮箱:1981462002@qq.com<br/> * 说明:缓存策略接口 */ public interface CacheStrategy { /** * 存储缓存 * @param key 文件名 * @param value 文件内容 * @param time 有效时间 单位:小时 */ void setCache(String key, String value,long time); /** * 获取缓存 * @param key 文件名 * @return 文件内容 */ String getCache(String key); } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:38<br/> * 邮箱:1981462002@qq.com<br/> * 说明:文件缓存基类 */ public abstract class BaseFileStrategy implements CacheStrategy { /** * 缓存文件的文件夹名称 */ private String mDirName; /** * 构造函数 * @param dirName 缓存文件的文件夹名称 */ public BaseFileStrategy(String dirName) { mDirName = dirName; } @Override public void setCache(String key, String value, long time) { // 以url为文件名, 以json为文件内容,保存在本地 // 生成缓存文件 File cacheFile = new File(mDirName + File.separator + Md5Util.getMD5(key)); FileWriter writer = null; try { if (!cacheFile.exists()) { cacheFile.getParentFile().mkdirs(); cacheFile.createNewFile(); } writer = new FileWriter(cacheFile); // 缓存失效的截止时间 long deadline = System.currentTimeMillis() + time * 60 * 60 * 1000;//有效期 writer.write(deadline + "\n");// 在第一行写入缓存时间, 换行 writer.write(value);// 写入文件 writer.flush(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.close(writer); } } @Override public String getCache(String key) { // 生成缓存文件 File cacheFile = new File(mDirName + File.separator + Md5Util.getMD5(key)); // 判断缓存是否存在 if (cacheFile.exists()) { // 判断缓存是否有效 BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(cacheFile)); String deadline = reader.readLine();// 读取第一行的有效期 long deadTime = Long.parseLong(deadline); if (System.currentTimeMillis() < deadTime) {// 当前时间小于截止时间, // 说明缓存有效 // 缓存有效 StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } else { setCache(key, "", 0);//缓存过期清空 } } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.close(reader); } } return null; } } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:28<br/> * 邮箱:1981462002@qq.com<br/> * 说明:以文件保存缓存 到本包 <br/> */ public class InnerFileStrategy extends BaseFileStrategy { public InnerFileStrategy(Context context) { super(context.getCacheDir().getPath()); } } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:28<br/> * 邮箱:1981462002@qq.com<br/> * 说明:以文件保存缓存 到SD卡cacheData目录 <br/> */ public class SDFileStrategy extends BaseFileStrategy { public SDFileStrategy() { super(Environment.getExternalStorageDirectory() +"/cacheData"); } } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:8:03<br/> * 邮箱:1981462002@qq.com<br/> * 说明:SharedPreferences缓存 */ public class SPStrategy implements CacheStrategy { private Context mContext; public SPStrategy(Context context) { mContext = context; } @Override public void setCache(String key, String value, long time) { SpUtils<String> spString= new SpUtils<>(mContext); spString.put(key, value); SpUtils<Long> spLong = new SpUtils<>(mContext); spLong.put("LiftTime", System.currentTimeMillis() + time * 60 * 60 * 1000); } @Override public String getCache(String key) { SpUtils<Long> spLong = new SpUtils<>(mContext); Long liftTime = spLong.get("LiftTime", 0L); if (System.currentTimeMillis() < liftTime) {// 当前时间小于截止时间, SpUtils<String> spString= new SpUtils<>(mContext); return spString.get(key, ""); }else { setCache(key, "", 0);//缓存过期清空 } return null; } } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:23<br/> * 邮箱:1981462002@qq.com<br/> * 说明:缓存工作类 */ public class CacheWorker { /** * 缓存策略 */ private CacheStrategy mCacheStrategy; /** * 无参构造 */ public CacheWorker() { } /** * 一参构造 * @param cacheStrategy 缓存策略 */ public CacheWorker(CacheStrategy cacheStrategy) { mCacheStrategy = cacheStrategy; } /** * 存储缓存 * @param key 文件名 * @param value 文件内容 * @param time 有效时间 单位:小时 */ public void setCache(String key, String value, long time) { mCacheStrategy.setCache(key, value, time); } /** * 获取缓存 * @param key 文件名 * @return 文件内容 */ public String getCache(String key) { return mCacheStrategy.getCache(key); } /** * 设置缓存策略 * @param cacheStrategy 缓存策略 */ public void setCacheStrategy(CacheStrategy cacheStrategy) { mCacheStrategy = cacheStrategy; } } 后记、 1.声明: [1]本文由张风捷特烈原创,转载请注明 [2]欢迎广大编程爱好者共同交流 [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正 [4]你的喜欢与支持将是我最大的动力 2.连接传送门: 更多安卓技术欢迎访问:安卓技术栈 我的github地址:欢迎star 简书首发,腾讯云+社区同步更新张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com 3.联系我 QQ:1981462002 邮箱:1981462002@qq.com 微信:zdl1994328 4.欢迎关注我的微信公众号,最新精彩文章,及时送达: 公众号.jpg

优秀的个人博客,低调大师

Android安卓比较完美的无限自动轮播

轮播功能在APP中是比较常见的,网上也有很多第三方的自动轮播库,做的很不错,这里自己手写一版简单的ViewPager+Handler机制的自动无限轮播,小白可以直接上手的。 先上简单的核心代码,代码中有详细的注释。 private void initView() { auto_viewpager = (ViewPager) findViewById(R.id.vp_shuffling); tv_content = (TextView) findViewById(R.id.tv_content); dotLayout = (LinearLayout) findViewById(R.id.layout_point); mADParseArray = new ArrayList<String>(); mADParseArray .add("http://m.easyto.com/m/zhulifuwu_banner.jpg"); mADParseArray .add("http://m.easyto.com/m/japan/images/banner_3y_new.jpg"); mADParseArray .add("http://m.easyto.com/m/japan/images/banner_5y_new.jpg"); final int size = mADParseArray.size(); content = new String[]{"轮播图介绍一", "轮播图介绍二", "轮播图介绍三"}; auto_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { /** * @param position:跳转完毕的页码角标,这个方法做自动轮播 */ @Override public void onPageSelected(int position) { Log.e(TAG, "onPageSelected"); refreshPoint(position % size); if (mHandler.hasMessages(HOME_AD_RESULT)) { mHandler.removeMessages(HOME_AD_RESULT); } mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3000); } /** * @param arg0:滑动时代表当前页角标,滑动结束时代表滑动停止时的页码角标 * @param arg1:0-1或者1-0变化 * @param arg2:0 <-> 屏幕的宽度值变化 */ @Override public void onPageScrolled(int arg0, float arg1, int arg2) { Log.e(TAG, "onPageScrolled"); } /** * @param arg0:0 没滑动 1:正在滑动 2:滑动完毕 */ @Override public void onPageScrollStateChanged(int arg0) { Log.e(TAG, "onPageScrollStateChanged = " + arg0); if (ViewPager.SCROLL_STATE_DRAGGING == arg0 && mHandler.hasMessages(HOME_AD_RESULT)) { mHandler.removeMessages(HOME_AD_RESULT); } } }); // BannerAdapter adapter = new BannerAdapter(mContext, mADParseArray); auto_viewpager.setAdapter(adapter); initPointsLayout(size); auto_viewpager.setCurrentItem(size * 1000, false);//设置当前position,不设置开始无法右滑 // 利用handler启动自动轮播 mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3000); } handler中 private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { // 广告 case HOME_AD_RESULT: auto_viewpager.setCurrentItem(auto_viewpager.getCurrentItem() + 1, true);//设置播放下一页 break; } } }; 其中比较重要的就是viewpager滑动监听的三个回调方法,注释上都有详细的解释,相信看了这个以后小白应该也会手写一个简单的无限自动轮播功能了。是不是简明易懂0.0 源码地址,直接导入modul即可

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册