緩存的方式有多種,最常用的類(lèi)似搜索記錄,這些用的數(shù)據(jù)庫(kù)比較多。
本文用的是一個(gè)數(shù)據(jù)庫(kù)框架GreenDao,正好也練習(xí)一下。
關(guān)于技術(shù)部分需要的操作也不是太多,無(wú)非包括兩部分:
一部分是在接到推送的消息的時(shí)候緩存,另一部分是在頁(yè)面的時(shí)候?qū)⑾⒄故境鰜?lái)。
但是有個(gè)缺點(diǎn),數(shù)據(jù)清除了之后,除非自己去后臺(tái)查看記錄,不然就被清理掉了。
源碼在GitHub如果有介紹不清楚的地方以去查看
https://github.com/wapchief/android-CollectionDemo
關(guān)于GreenDao的介紹這里就不詳細(xì)描述了。
直接開(kāi)始代碼部分。
1、需要集成GreenDao,和極光JPush兩個(gè)sdk。
Modle App:
apply plugin: 'org.greenrobot.greendao'
......
greendao {
schemaVersion 1//數(shù)據(jù)庫(kù)版本號(hào)
daoPackage 'wapchief.com.collectiondemo.greendao'//設(shè)置DaoMaster、DaoSession、Dao包名
targetGenDir 'src/main/java'//設(shè)置DaoMaster、DaoSession、Dao目錄
//targetGenDirTest:設(shè)置生成單元測(cè)試目錄
//generateTests:設(shè)置自動(dòng)生成單元測(cè)試用例
}
......
dependencies {
compile 'cn.jiguang.sdk:jpush:3.0.3' // 此處以JPush 3.0.3 版本為例。
compile 'cn.jiguang.sdk:jcore:1.1.1' // 此處以JCore 1.1.1 版本為例。
compile 'org.greenrobot:greendao:3.2.0'
}
Project:
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'
// in the individual module build.gradle files
}
建議參考:
Android SDK 集成指南
GreenDao官方文檔
在集成GreenDao的時(shí)候有可能被墻,導(dǎo)致下載不下來(lái),可以修改代理為127.0.0.1
2、創(chuàng)建實(shí)體類(lèi)用于生成數(shù)據(jù)庫(kù)
/**
* Created by Wu on 2017/5/11 0011 上午 9:24.
* 描述:存放極光推送的消息
*/
@Entity
public class Message {
@Id(autoincrement = true)
private Long id;
private String title;
private String content;
@Override
public String toString() {
return "Message{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
@Generated(hash = 977969778)
public Message(Long id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
@Generated(hash = 637306882)
public Message() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
創(chuàng)建之后,運(yùn)行Build > Make Module app,會(huì)生成數(shù)據(jù)庫(kù)操作的DAO類(lèi)。
如果實(shí)體是Message,那么自動(dòng)生成的就是MessageDao.
3、初始化數(shù)據(jù)庫(kù)相關(guān)
private void initDbHelp() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(BaseApplication.mBaseApplication, "recluse-db", null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
messageDao = daoSession.getMessageDao();
}
4、在廣播器里操作
在集成JPush的時(shí)候需要在本地建立本地廣播繼承BroadcastReceiver,一般命名為MyReceiver
public class MyReceiver extends BroadcastReceiver{
private static final String TAG = "JPush";
MessageDao messageDao;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
//初始化數(shù)據(jù)庫(kù)
initDbHelp();
.......
else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下來(lái)的通知:"+bundle.getString(JPushInterface.EXTRA_ALERT));
String content = bundle.getString(JPushInterface.EXTRA_ALERT);
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
//獲取當(dāng)前時(shí)間
String str = formatter.format(curDate);
messageDao.insert(new Message(null, str, content));
Log.d(TAG, "[MyReceiver] 接收到推送下來(lái)的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用戶(hù)點(diǎn)擊打開(kāi)了通知");
//打開(kāi)自定義的Activity
Intent i = new Intent(context, MessageActivity.class);
i.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
context.startActivity(i);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用戶(hù)收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在這里根據(jù) JPushInterface.EXTRA_EXTRA 的內(nèi)容處理代碼,比如打開(kāi)新的Activity, 打開(kāi)一個(gè)網(wǎng)頁(yè)等..
} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
} else {
Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
}
我們所收到的推送就在這里進(jìn)行處理。
通過(guò)JPushInterface.EXTRA_ALERT獲取系統(tǒng)推送的消息。
然后使用
messageDao.insert(new Message(null, str, content));
將消息存放到數(shù)據(jù)庫(kù),為了區(qū)分,這里加了一個(gè)推送的時(shí)間。
這時(shí)候數(shù)據(jù)庫(kù)已經(jīng)存在了該條消息。
還有個(gè)點(diǎn)擊通知的操作,一般是跳轉(zhuǎn)到消息列表或者詳情。
直接在里面寫(xiě)事件即可。
5、在Activity里展示消息
private void initview() {
//查詢(xún)所有
list = messageDao.queryBuilder().list();
//list倒序排列
Collections.reverse(list);
adapter = new ItemTVAdapter(context, list);
messageLv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
這里只需要一個(gè)簡(jiǎn)單的查詢(xún)?nèi)康恼Z(yǔ)句就可以。
