Content Provider not Sync with SqliteDatabase

I have 2 activities.

ActivityA accesses database through Content Provider, and it start ActivityB .

ActivityB accesses database directly.

I found after ActivityB updated the database, ActivityA querying database by CP and the result won't update.

But the database is actually updated!

How to sync the two methods?

PS: ActivityA and ActivityB are in different Applications.


Content providers when you update as a courtesy call NotifyChange This is to notify subscribers of your content provider that content has changed.

Activity A needs to access the content provider by URI and register a content observer. Activity B needs to call NotifyChange with a row based URI so that observers of the change will receive a row based URI. Everything depends on the URI used to access the contentprovider. This gives realtime changes bA. B changes >>> A receives the changes and can do something with them.

Notification/Event for row and column level observation in Content Provider

Reload or notify Fragment's data in ViewPager

Content Provider Update


** database **

  @Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub


    db.execSQL("CREATE TABLE " + TABLE_NAME
            + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " + key_msg + " STRING, " + key_isread + " STRING)");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

//*************************-------INSERT GCM MESSAGE---------**************************//

public void insert_GCM_receive_data(String msg) {

    String value;
    SQLiteDatabase db = getWritableDatabase();

    ContentValues cv = new ContentValues();

    cv.put(key_msg, msg);
    cv.put(key_isread, "N");

    value = cv.toString();

    db.insert(TABLE_NAME, null, cv);
    System.out.println("/n******this is temp table name  " + TABLE_NAME + "nthis is temp msg  " + cv + "nmsg" + msg + "nval" + value);

    db.close();

}

//-----------------------------------------------------------------------------------//

//**********************----------GET ALERT DATA------------**************************// public ArrayList get_alert_msg() {

    ArrayList<String> name = new ArrayList<String>();
    try {
        SQLiteDatabase db = getWritableDatabase();
        Cursor c = null;
        c = db.rawQuery("SELECT  * FROM " + TABLE_NAME, null);

        System.out.println(c);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

            String str_id = c.getString(0);
            String str_msg = c.getString(1);
            String str_read = c.getString(2);
            Log.e("value", str_id + str_msg + str_read);

            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("msg", str_msg);
            hm.put("isread", str_read);


            name.add(str_msg);


        }
        c.close();
        db.close();

    } catch (Exception e) {
        Log.e("this not work", "" + e);
    }

    return name;
}

//call method like in 2 activity dbHelper.get_alert_msg(); ....

Data manipulation and retrieve operation use with in the Database class... Once you close the db after retrieve data . Use common database method to two activity.. I hope this will help you..


确保在使用更新完成活动b时,在游标或db助手上调用close()

链接地址: http://www.djcxy.com/p/37784.html

上一篇: 如何使Angular2服务单身人士?

下一篇: 内容提供者不与SqliteDatabase同步