当前位置:主页 > 移动开发 > Android代码 >

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

时间:2022-03-11 08:51:23 | 栏目:Android代码 | 点击:

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

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

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

设置缓存策略如下:

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 coming 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 is coming from cache.

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

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

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

2、Offline storage离线存储

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

使用脱机:

App42API.setofflineStorage(true); 

案例代码:

//Set Offline Storage to True
App42API.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());
}
});

您可能感兴趣的文章:

相关文章