在android中使用緩存和脫機(jī)存儲(chǔ)
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)文章
android自定義控件ImageView實(shí)現(xiàn)圓形圖片
這篇文章主要為大家詳細(xì)介紹了android自定義控件ImageView實(shí)現(xiàn)圓形圖片,適用于用戶頭像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12RxJava入門指南及其在Android開發(fā)中的使用示例
RxJava是JVM的一個(gè)擴(kuò)展庫,它能夠幫助Java更加方便地實(shí)現(xiàn)基于事件的編程,這對安卓來說十分有用,接下來就一起來看一下RxJava入門指南及其在Android開發(fā)中的使用示例:2016-06-06Android開發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問題分析
這篇文章主要介紹了Android開發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問題,結(jié)合實(shí)例形式分析了Android三角函數(shù)運(yùn)算中的弧度與角度計(jì)算問題與相關(guān)解決方法,需要的朋友可以參考下2017-11-11Android 8.0系統(tǒng)中應(yīng)用圖標(biāo)的適配微技巧
這篇文章主要介紹了Android 8.0系統(tǒng)中應(yīng)用圖標(biāo)的適配微技巧 ,需要的朋友可以參考下2018-04-04Android 調(diào)用系統(tǒng)相機(jī)拍攝獲取照片的兩種方法實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android 調(diào)用系統(tǒng)相機(jī)拍攝獲取照片的兩種方法實(shí)現(xiàn)實(shí)例的相關(guān)資料,一種是通過Bundle來獲取壓縮過的照片,一種是通過SD卡獲取的原圖,需要的朋友可以參考下2016-11-11android 實(shí)現(xiàn)類似微信緩存和即時(shí)更新好友頭像示例
本篇文章主要介紹了android 實(shí)現(xiàn)類似微信緩存和即時(shí)更新好友頭像示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果
這篇文章主要給大家介紹了關(guān)于Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12