在android中如何使用缓存和脱机存储


这篇文章将为大家详细讲解有关在android中如何使用缓存和脱机存储,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

1、在android中使用缓存和脱机存储

  缓存可以加速你的应用程序,即使在网络不可用时,用户能够更加流畅地使用你的应用程序使用缓存是相当简单的,需要一个单一的代码行。

导入 import.shephertz.app42.paas.sdk.android.App42CacheManager即可,同时需要设置缓存策略。

  •  Policy.CACHE_FIRSTSetting 将激活所有数据的读操作首先从缓存中获取,如果缓存中数据可用且没有失效,就直接从缓存返回,否则进行网络请求这个数据,同时将这个数据更新或加入缓存中,你可以通过API设置缓存失效期,缺省是1一个小时。

  • Policy.NETWORK_FIRST 首先从网络获取数据,然后更新缓存。如果网络不可用,数据就从缓存中取出

  • cache.Policy.NOCACHEBy 这是App42 SDK默认,不使用任何缓存,总是仅从网络读数据。

设置缓存策略如下:

App42CacheManager.setPolicy(Policy.CACHE_FIRST);

缓存失效期:

App42CacheManager.setExpiryInMinutes(<EXPIRY_TIME_IN_MINUTES>);

案例代码如下:

               UserService userService = App42API.buildUserService();               String userName = "Nick";              userService.getUser(userName,new App42CallBack() {               public void onSuccess(Object response)                        {                     User user = (User)response;                     if(user.isFromCache()){                            //Response&nbsping from Cache                                           System.out.println("userName is " + user.getUserName());                             System.out.println("emailId is " + user.getEmail());                            System.out.println("is from cache is " + user.isFromCache());                                               }                     else{                            //Response From Server                            System.out.println("userName is " + user.getUserName());                             System.out.println("emailId is " + user.getEmail());                     }                                    }              public void onException(Exception ex)              {                     System.out.println("Exception Message"+ex.getMessage());              }              });

If Response is from cache you will get isFromCache flag to true in the response so you can identify that data is real time or data ising from cache.

如果响应来自缓存,你在响应中通过isFromCache标识为true,这样你能分辨数据是实时的还是来自缓存的。

下面是需要在manifest.xml加入的:

          <receiver android:name=&quot.shephertz.app42.paas.sdk.android.App42BroadcastReceiver">      <intent-filter>        <action android:name="android.intent.action.BOOTPLETED"/>      </intent-filter>    </receiver>    <receiver android:name=&quot.shephertz.app42.paas.sdk.android.AlarmReceiver"/>          <service android:name=&quot.shephertz.app42.paas.sdk.android.App42DataSyncService"/>

 2、Offline storage离线存储

  离线存储允许你在本地网络的情况下不可用提交数据,当网络可用时,服务器会同步。这在许多情况下是非常有用的,例如如果你的用户玩游戏,并取得了一些特定级别的完成。然而,在发送成绩时,网络断了,那么他的得分可能会丢失,使用脱机缓存会在本地网络无法获得情况下,保存他的得分,并将于稍后网络恢复可用时与同步服务器的。

使用脱机:

App42API.setofflineStorage(true);

案例代码:

//Set Offline Storage to TrueApp42API.setofflineStorage(true);String gameName = "<Enter_your_game/level_name>";String userName = "Nick";BigDecimal gameScore = new BigDecimal(3500);scoreBoardService.saveUserScore(gameName, userName, gameScore,new App42CallBack() {public void onSuccess(Object response){       Game game = (Game)response;       if(game.isOfflineSync())        {     //Request is saved in cache              System.out.println("Information is Stored in cache, will send to App42 when network is available");       }       else {              //Response Received From Server and is Succeseful              System.out.println("Server Response : " + game);       }}public void onException(Exception ex){       System.out.println("Exception Message"+ex.getMessage());}});

关于在android中如何使用缓存和脱机存储就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


上一篇:vuejs如何实现字符串转日期

下一篇:怎么用Java实现synchronized锁同步机制


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器