欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在android中使用緩存和脫機(jī)存儲(chǔ)

 更新時(shí)間:2021年11月04日 11:36:57   作者:banq  
這篇文章主要介紹了在android中使用緩存和脫機(jī)存儲(chǔ),緩存可以加速你的應(yīng)用程序,即使在網(wǎng)絡(luò)不可用時(shí),用戶能夠更加流暢地使用你的應(yīng)用程序使用緩存是相當(dāng)簡單的,需要一個(gè)單一的代碼行,下面來看看文章的詳細(xì)內(nèi)容

1、在android中使用緩存和脫機(jī)存儲(chǔ)

  緩存可以加速你的應(yīng)用程序,即使在網(wǎng)絡(luò)不可用時(shí),用戶能夠更加流暢地使用你的應(yīng)用程序使用緩存是相當(dāng)簡單的,需要一個(gè)單一的代碼行。

導(dǎo)入 import com.shephertz.app42.paas.sdk.android.App42CacheManager即可,同時(shí)需要設(shè)置緩存策略。

  •  Policy.CACHE_FIRSTSetting 將激活所有數(shù)據(jù)的讀操作首先從緩存中獲取,如果緩存中數(shù)據(jù)可用且沒有失效,就直接從緩存返回,否則進(jìn)行網(wǎng)絡(luò)請求這個(gè)數(shù)據(jù),同時(shí)將這個(gè)數(shù)據(jù)更新或加入緩存中,你可以通過API設(shè)置緩存失效期,缺省是1一個(gè)小時(shí)。
  • Policy.NETWORK_FIRST 首先從網(wǎng)絡(luò)獲取數(shù)據(jù),然后更新緩存。如果網(wǎng)絡(luò)不可用,數(shù)據(jù)就從緩存中取出
  • cache.Policy.NOCACHEBy 這是App42 SDK默認(rèn),不使用任何緩存,總是僅從網(wǎng)絡(luò)讀數(shù)據(jù)。

設(shè)置緩存策略如下:

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.

如果響應(yīng)來自緩存,你在響應(yīng)中通過isFromCache標(biāo)識(shí)為true,這樣你能分辨數(shù)據(jù)是實(shí)時(shí)的還是來自緩存的。

下面是需要在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離線存儲(chǔ)

  離線存儲(chǔ)允許你在本地網(wǎng)絡(luò)的情況下不可用提交數(shù)據(jù),當(dāng)網(wǎng)絡(luò)可用時(shí),服務(wù)器會(huì)同步。這在許多情況下是非常有用的,例如如果你的用戶玩游戲,并取得了一些特定級(jí)別的完成。然而,在發(fā)送成績時(shí),網(wǎng)絡(luò)斷了,那么他的得分可能會(huì)丟失,使用脫機(jī)緩存會(huì)在本地網(wǎng)絡(luò)無法獲得情況下,保存他的得分,并將于稍后網(wǎng)絡(luò)恢復(fù)可用時(shí)與同步服務(wù)器的。

使用脫機(jī):

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());
}
});

到此這篇關(guān)于在android中使用緩存和脫機(jī)存儲(chǔ)的文章就介紹到這了,更多相關(guān)android使用緩存和脫機(jī)存儲(chǔ)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論