緩存的方式有多種,最常用的類似搜索記錄,這些用的數據庫比較多。
本文用的是一個數據庫框架GreenDao,正好也練習一下。
關于技術部分需要的操作也不是太多,無非包括兩部分:
一部分是在接到推送的消息的時候緩存,另一部分是在頁面的時候將消息展示出來。
但是有個缺點,數據清除了之后,除非自己去后臺查看記錄,不然就被清理掉了。
源碼在GitHub如果有介紹不清楚的地方以去查看
https://github.com/wapchief/android-CollectionDemo
關于GreenDao的介紹這里就不詳細描述了。
直接開始代碼部分。
1、需要集成GreenDao,和極光JPush兩個sdk。
Modle App:
apply plugin: 'org.greenrobot.greendao'
......
greendao {
schemaVersion 1//數據庫版本號
daoPackage 'wapchief.com.collectiondemo.greendao'//設置DaoMaster、DaoSession、Dao包名
targetGenDir 'src/main/java'//設置DaoMaster、DaoSession、Dao目錄
//targetGenDirTest:設置生成單元測試目錄
//generateTests:設置自動生成單元測試用例
}
......
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的時候有可能被墻,導致下載不下來,可以修改代理為127.0.0.1
2、創建實體類用于生成數據庫
/**
* 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;
}
}
創建之后,運行Build > Make Module app,會生成數據庫操作的DAO類。
如果實體是Message,那么自動生成的就是MessageDao.
3、初始化數據庫相關
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的時候需要在本地建立本地廣播繼承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();
//初始化數據庫
initDbHelp();
.......
else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下來的通知:"+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());
//獲取當前時間
String str = formatter.format(curDate);
messageDao.insert(new Message(null, str, content));
Log.d(TAG, "[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用戶點擊打開了通知");
//打開自定義的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] 用戶收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在這里根據 JPushInterface.EXTRA_EXTRA 的內容處理代碼,比如打開新的Activity, 打開一個網頁等..
} 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());
}
}
我們所收到的推送就在這里進行處理。
通過JPushInterface.EXTRA_ALERT
獲取系統推送的消息。
然后使用
messageDao.insert(new Message(null, str, content));
將消息存放到數據庫,為了區分,這里加了一個推送的時間。
這時候數據庫已經存在了該條消息。
還有個點擊通知的操作,一般是跳轉到消息列表或者詳情。
直接在里面寫事件即可。
5、在Activity里展示消息
private void initview() {
//查詢所有
list = messageDao.queryBuilder().list();
//list倒序排列
Collections.reverse(list);
adapter = new ItemTVAdapter(context, list);
messageLv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
這里只需要一個簡單的查詢全部的語句就可以。
效果圖