我需要从GCM获取消息,然后将它们存储在数据库中。实际上,重点是获取消息然后更改片段中的Recycler视图。因此,当这些通知即将发布时,它们会出现在片段中(所以服务在后台运行并保存消息)。我设置了一个gcmlistener服务,它具有onMessageReceived
方法和数据库帮助程序类。但是这个数据库帮助器需要一个上下文,那么如何从GCMlistenerService
传递一个上下文呢?
public class MyGcmListenerService extends GcmListenerService {
private static final String TAG = "MyGcmListenerService";
mySQLAdapter adapter = new mySQLAdapter(/*What to put here*/);
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("price");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
sendNotification(message);
String time = DateFormat.getTimeInstance().toString();
long id = adapter.insertData("Information",message,time);
if (id<0)
Log.d("GCM","Unsuccess");
else
Log.d("GCM","Success");
Intent intent = new Intent("token");
intent.putExtra("message",message);
intent.putExtra("typeOf","Info");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
public class mySQLAdapter {
mySQLHelper helper;
public mySQLAdapter(Context context) {
helper = mySQLHelper.getmInstance(context);
}
public long insertData(String type, String description, String time) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(helper.TYPE, type);
contentValues.put(helper.DESCRIPTION, description);
contentValues.put(helper.TIME, time);
long id = db.insert(mySQLHelper.TABLE_NAME, null, contentValues);
return id;
}
public List<Notification> getData() {
List<Notification> notifications = new ArrayList<>();
SQLiteDatabase db = helper.getWritableDatabase();
String[] columns = {mySQLHelper.UID, mySQLHelper.TYPE, mySQLHelper.DESCRIPTION, mySQLHelper.TIME};
Cursor cursor = db.query(mySQLHelper.TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()) {
String type = cursor.getString(cursor.getColumnIndex(mySQLHelper.TYPE));
String desc = cursor.getString(cursor.getColumnIndex(mySQLHelper.DESCRIPTION));
String time = cursor.getString(cursor.getColumnIndex(mySQLHelper.TIME));
Notification notification = new Notification(type, desc, time, 0);
notifications.add(notification);
}
return notifications;
}
public static class mySQLHelper extends SQLiteOpenHelper {
private static mySQLHelper mInstance = null;
private static final String DATABASE_NAME = "notificationsDB";
private static final String TABLE_NAME = "notifications";
private static final String UID = "_id";
private static final int VERSION = 0;
private static final String TYPE = "Type";
private static final String DESCRIPTION = "Description";
private static final String TIME = "Time";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME
+ " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TYPE + " VARCHAR(255), " + DESCRIPTION + " VARCHAR(255), "
+ TIME + " VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
Context context;
public static mySQLHelper getmInstance(Context context) {
if (mInstance == null)
mInstance = new mySQLHelper(context.getApplicationContext());
return mInstance;
}
public mySQLHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
this.context = context;
Log.d("SQL", "constructor Called!");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
Log.d("SQL", "Helper onCreate called");
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
Log.d("SQL", "" + e);
Log.d("SQL", "" + e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Log.d("SQL", "Helper onUpgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
Log.d("SQL", "" + e);
Log.d("SQL", "" + e);
}
}
}
}