How to load data caching in Android _android

Source: Internet
Author: User

Recently the app is almost finished, but a lot of list loading, news consulting and other data has been requested from the network, slow, affecting the user experience, so thinking to use the cache to load some update requirements not too high data

First, make a tool class that saves the cache

Import Java.io.File; 
Import java.io.IOException;
Import Android.content.Context; 
Import android.os.Environment; 
Import Android.util.Log; 
/** * Cache Tool class/public class Configcacheutil {private static final String tag=configcacheutil.class.getname (); /** 1-second timeout/public static final int config_cache_short_timeout=1000 * 60 * 5; 5 min/** 5 minutes timeout/public static final int config_cache_medium_timeout=1000 * 3600 * 2; 2 hours/** Medium long cache time/public static final int config_cache_ml_timeout=1000 * 60 * 60 * 24 * 1; 1 days/** Maximum cache time/public static final int config_cache_max_timeout=1000 * 60 * 60 * 24 * 7; 7 Days/** * Config_cache_model_long: Long time (7 days) cache mode <br> * CONFIG_CACHE_MODEL_ML: Medium long time (12 hours) Cache mode <br> * CONFI G_cache_model_medium: Medium Time (2 hours) cache mode <br> * Config_cache_model_short: Short time (5 minutes) cache mode/public enum Configcachemode 
l {config_cache_model_short, config_cache_model_medium, config_cache_model_ml, Config_cache_model_long; /** * Get Cache * @param URL Access Network URL * @return Cached data */public static string Geturlcache (String URL, configcachemodel model,context context) { 
if (url = = null) {return null; 
} String Result=null; 
String Path=constant.enviroment_dir_cache + md5utils.md5encrypt (URL) + ". txt"; 
File File=new file (path); 
if (file.exists () && file.isfile ()) {long Expiredtime=system.currenttimemillis ()-file.lastmodified (); 
LOG.D (TAG, File.getabsolutepath () + "ExpiredTime:" + expiredtime/60000 + "min"); 1. If the system time is not correct//2. 
When the network is invalid, you can only read cache if (uiutils.isnetworkconnected) {if (ExpiredTime < 0) {return null; } if (model = = Configcachemodel.config_cache_model_short) {if (ExpiredTime > Config_cache_short_timeout) {return nul 
L } else if (model = = Configcachemodel.config_cache_model_medium) {if (ExpiredTime > Config_cache_medium_timeout) {r 
Eturn null; } else if (model = = CONFIGCACHEMODEL.CONFIG_CACHE_MODEL_ML) {if (ExpiredTime > Config_cache_ml_timeout) {return nu 
ll }} ELSE if (model = = Configcachemodel.config_cache_model_long) {if (ExpiredTime > Config_cache_medium_timeout) {return nu 
ll 
} else {if (ExpiredTime > Config_cache_max_timeout) {return null; 
}}} try {result=fileutils.readtextfile (file); 
catch (IOException e) {e.printstacktrace (); 
} return result; /** * Set Cache * @param data * @param url/public static void Seturlcache (string data, string url) {if constant.en 
Viroment_dir_cache = = null) {return; 
} file Dir=new file (Constant.enviroment_dir_cache); 
if (!dir.exists () && environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)) { 
Dir.mkdirs (); 
File File=new file (Constant.enviroment_dir_cache + md5utils.md5encrypt (URL) + ". txt"); 
try {///create cached data to disk, is to create file Fileutils.writetextfile (files, data); 
catch (IOException e) {log.d (TAG, "write" + file.getabsolutepath () + "Data failed!"); 
E.printstacktrace (); 
catch (Exception e) {e.printstacktrace (); } 
} 
/** 
* Delete history cache file * @param cachefile */public static void ClearCache (file cachefile) {if (cachefile = = null) {if environmen T.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)) {try {file Cachedir=new file ( 
Environment.getexternalstoragedirectory (). GetPath () + "/gdxz/cache/"); 
if (cachedir.exists ()) {ClearCache (cachedir); 
} catch (Exception e) {e.printstacktrace (); 
}} else if (Cachefile.isfile ()) {cachefile.delete (); 
else if (cachefile.isdirectory ()) {file[] childfiles=cachefile.listfiles (); 
for (int i=0 i < childfiles.length; i++) {ClearCache (childfiles[i)); } 
} 
} 
}

A tool that has been written, simply call the Configcacheutil.geturlcache method in the place where the cache is needed to pass in the appropriate URL, type and context to determine whether the result is null, and if NULL indicates that there is no cache request Network load data. If there is no null description of the data in the cache directly read cache, get result and processing JSON data is OK

The above is the way to load data caching in Android, and hopefully it will help.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.