コード例 #1
0
ファイル: ConfigCache.java プロジェクト: LinQingWei/WayHoo
  public static String getUrlCache(Context context, String url) {
    if (TextUtils.isEmpty(url)) {
      return null;
    }
    int netState = NetUtil.getNetworkState(context);

    File file = new File(getCacheDir(context) + File.separator + replaceUrlWithPlus(url));
    if (file.exists() && file.isFile()) {
      long expiredTime = System.currentTimeMillis() - file.lastModified();
      Log.i("liweiping", url + ": expiredTime=" + expiredTime / 1000);
      // 1. in case the system time is incorrect (the time is turn back
      // long ago)
      // 2. when the network is invalid, you can only read the cache
      if (netState != NetUtil.NETWORN_NONE && expiredTime < 0) {
        return null;
      }
      // 如果是wifi网络,则30分钟过期
      if (netState == NetUtil.NETWORN_WIFI && expiredTime > CONFIG_CACHE_WIFI_TIMEOUT) {
        return null;
        // 如果是手机网络,则2个小时过期
      } else if (netState == NetUtil.NETWORN_MOBILE && expiredTime > CONFIG_CACHE_MOBILE_TIMEOUT) {
        return null;
      }
      try {
        String result = FileUtils.readTextFile(file);
        return result;
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return null;
  }