0
我試圖將gcm消息保存在數據庫中,但每次發送消息時,應用程序都會關閉。保存在數據庫中的GCM消息錯誤
這裏是我的GCMIntentService onMessage方法:
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("Memo");
displayMessage(context, message);
MemoDBAdapterHandler db = new MemoDBAdapterHandler(context);
db.saveMessage(message);
generateNotification(context, message);
}
這裏是MemoDBAdapterHandler
公共類MemoDBAdapterHandler {
public static final String KEY_BODY = "Body";
public static final String KEY_ROWID ="_id1";
private static final String TAG = "MemoDBAdapterHandler";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
/** CreateMemoDatabase **/
private static final String DATABASE_CREATE =
"create table memosTable1 (KEY_BODY integer primary key autoincrement, "
+ "Body string not null);";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "GCM2";
private static final String DATABASE_TABLE = "memosTable1";
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS memosTable");
onCreate(db);
}
}
public MemoDBAdapterHandler(Context ctx)
{
this.mCtx = ctx;
}
public MemoDBAdapterHandler open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long saveMessage(String msg)
{
//SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_BODY, msg);
//mDb.close();
return mDb.insert(DATABASE_TABLE, null, cv);
}
我已經改變它,但應用程序仍然關閉。 – user3261166
在try catch塊中調用saveMessage並檢查它給出的錯誤。 –
也從設備上卸載應用程序,然後運行新的apk文件或升級數據庫版本 –