吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4980|回复: 1
收起左侧

[会员申请] 申请会员ID:abcdyfj522

[复制链接]
吾爱游客  发表于 2019-5-11 02:03
1、abcdyfj522
2、个人邮箱:1009395239@qq.com
3、原创技术文章:安卓开发文件缓存方法的具体实现(参考代码)



安卓开发中使用缓存,基本介绍请参见: 《安卓开发中的缓存管理》

    安卓开发中设置文件缓存的方法,参考代码如下:

  1 public class ConfigCache {
  2     private static final String TAG = ConfigCache.class.getName();
  3   
  4     public static final int CONFIG_CACHE_MOBILE_TIMEOUT  = 3600000;  //1 hour
  5     public static final int CONFIG_CACHE_WIFI_TIMEOUT    = 300000;   //5 minute
  6   
  7     public static String getUrlCache(String url) {
  8         if (url == null) {
  9             return null;
10         }
11   
12         String result = null;
13         File file = new File(AppApplication.mSdcardDataDir + "/" + getCacheDecodeString(url));
14         if (file.exists() && file.isFile()) {
15             long expiredTime = System.currentTimeMillis() - file.lastModified();
16             Log.d(TAG, file.getAbsolutePath() + " expiredTime:" + expiredTime/60000 + "min");
17             //1. in case the system time is incorrect (the time is turn back long ago)
18             //2. when the network is invalid, you can only read the cache
19             if (AppApplication.mNetWorkState != NetworkUtils.NETWORN_NONE && expiredTime < 0) {
20                 return null;
21             }
22             if(AppApplication.mNetWorkState == NetworkUtils.NETWORN_WIFI
23                    && expiredTime > CONFIG_CACHE_WIFI_TIMEOUT) {
24                 return null;
25             } else if (AppApplication.mNetWorkState == NetworkUtils.NETWORN_MOBILE
26                    && expiredTime > CONFIG_CACHE_MOBILE_TIMEOUT) {
27                 return null;
28             }
29             try {
30                 result = FileUtils.readTextFile(file);
31             } catch (IOException e) {
32                 e.printStackTrace();
33             }
34         }
35         return result;
36     }
37   
38     public static void setUrlCache(String data, String url) {
39         File file = new File(AppApplication.mSdcardDataDir + "/" + getCacheDecodeString(url));
40         try {
41             //创建缓存数据到磁盘,就是创建文件
42             FileUtils.writeTextFile(file, data);
43         } catch (IOException e) {
44             Log.d(TAG, "write " + file.getAbsolutePath() + " data failed!");
45             e.printStackTrace();
46         }
47     }
48   
49     public static String getCacheDecodeString(String url) {
50         //1. 处理特殊字符
51         //2. 去除后缀名带来的文件浏览器的视图凌乱(特别是图片更需要如此类似处理,否则有的手机打开图库,全是我们的缓存图片)
52         if (url != null) {
53             return url.replaceAll("[.:/,%?&=]", "+").replaceAll("[+]+", "+");
54         }
55         return null;
56     }
57 }
58
59
    上面的方法如何调用,参考代码如下:

  1 void getConfig(){
  2         //首先尝试读取缓存
  3         String cacheConfigString = ConfigCache.getUrlCache(CONFIG_URL);
  4         //根据结果判定是读取缓存,还是重新读取
  5         if (cacheConfigString != null) {
  6             showConfig(cacheConfigString);
  7         } else {
  8             //如果缓存结果是空,说明需要重新加载
  9             //缓存为空的原因可能是1.无缓存;2. 缓存过期;3.读取缓存出错
10             AsyncHttpClient client = new AsyncHttpClient();
11             client.get(CONFIG_URL, new AsyncHttpResponseHandler(){
12                 @Override
13                 public void onSuccess(String result){
14                     //成功下载,则保存到本地作为后面缓存文件
15                     ConfigCache.setUrlCache(result,  CONFIG_URL);
16                     //后面可以是UI更新,仅供参考
17                     showConfig(result);
18                 }
19                 @Override
20                 public void onFailure(Throwable arg0) {
21                     //根据失败原因,考虑是显示加载失败,还是再读取缓存
22                 }
23             });
24         }
25     }
26
27

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Hmily 发表于 2019-5-12 22:23
抱歉,未能达到申请要求,申请不通过,可以关注论坛官方微信(吾爱破解论坛),等待开放注册通知。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-23 15:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表