在android中使用緩存和脫機(jī)存儲(chǔ)
1、在android中使用緩存和脫機(jī)存儲(chǔ)
緩存可以加速你的應(yīng)用程序,即使在網(wǎng)絡(luò)不可用時(shí),用戶(hù)能夠更加流暢地使用你的應(yīng)用程序使用緩存是相當(dāng)簡(jiǎn)單的,需要一個(gè)單一的代碼行。
導(dǎo)入 import com.shephertz.app42.paas.sdk.android.App42CacheManager即可,同時(shí)需要設(shè)置緩存策略。
Policy.CACHE_FIRSTSetting將激活所有數(shù)據(jù)的讀操作首先從緩存中獲取,如果緩存中數(shù)據(jù)可用且沒(méi)有失效,就直接從緩存返回,否則進(jìn)行網(wǎng)絡(luò)請(qǐng)求這個(gè)數(shù)據(jù),同時(shí)將這個(gè)數(shù)據(jù)更新或加入緩存中,你可以通過(guò)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)來(lái)自緩存,你在響應(yīng)中通過(guò)isFromCache標(biāo)識(shí)為true,這樣你能分辨數(shù)據(jù)是實(shí)時(shí)的還是來(lái)自緩存的。
下面是需要在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ì)同步。這在許多情況下是非常有用的,例如如果你的用戶(hù)玩游戲,并取得了一些特定級(jí)別的完成。然而,在發(fā)送成績(jī)時(shí),網(wǎng)絡(luò)斷了,那么他的得分可能會(huì)丟失,使用脫機(jī)緩存會(huì)在本地網(wǎng)絡(luò)無(wú)法獲得情況下,保存他的得分,并將于稍后網(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android onMeasure與onDraw及自定義屬性使用示例
這篇文章主要介紹了Android onMeasure與onDraw及自定義屬性使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02
Android繼承現(xiàn)有控件拓展實(shí)現(xiàn)自定義控件textView
這篇文章主要介紹了Android繼承現(xiàn)有控件拓展實(shí)現(xiàn)自定義控件textView的相關(guān)資料,需要的朋友可以參考下2016-04-04
代碼分析Android實(shí)現(xiàn)側(cè)滑菜單
現(xiàn)在app越來(lái)越注重用戶(hù)體驗(yàn),本文給大家分析android實(shí)現(xiàn)側(cè)滑菜單的代碼,代碼簡(jiǎn)單易懂,感興趣的朋友一起看看吧2015-11-11
Kotlin協(xié)程Job生命周期結(jié)構(gòu)化并發(fā)詳解
這篇文章主要為大家介紹了Kotlin協(xié)程Job生命周期結(jié)構(gòu)化并發(fā)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
詳解Android應(yīng)用main函數(shù)的調(diào)用
Android常識(shí),App主線程初始化了Looper,調(diào)用prepare的地方是ActivityThread.main函數(shù)。問(wèn)題來(lái)了,App的main函數(shù)在哪兒調(diào)用,下面我們來(lái)一起學(xué)習(xí)一下吧2019-06-06
Android檢測(cè)url地址是否可達(dá)的兩種方法
今天小編就為大家分享一篇Android檢測(cè)url地址是否可達(dá)的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Android自定義有限制區(qū)域圖例角度自識(shí)別涂鴉工具類(lèi)
這篇文章主要為大家介紹了Android自定義有限制區(qū)域圖例角度自識(shí)別涂鴉工具類(lèi),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android中Bitmap、File與Uri之間的簡(jiǎn)單記錄
這篇文章主要給大家介紹了關(guān)于Android中Bitmap、File與Uri之間的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

