0

如何在sqlite插入/更新完成後觸發sendBroadcast? BroadCastReceiver得到正確的廣播。如何在sqlite插入/更新完成後觸發sendBroadcast?

我這樣做,但sendBroadcast在插入/更新完成前觸發。

編輯

更具體地講,這裏是更多的代碼,我該怎麼做:

MyActivity.java

public static final String[][] KEYS_TYPES = { 
     new String[] { "id_field", "int"}, 
     new String[] { "text", "string" } 
     ................. and so on.................. 
} 

// Get rows from mysql database as JSONArray 

DBDatabase db = new DBDatabase(context); 
db.addAll(rows, "myTable", "id_field", KEYS_TYPES); 

DBDatabase.java

public class DBDatabase extends SQLiteOpenHelper { 
    public static final String DATABASE_NAME = "myDB"; 
    private SQLiteDatabase db; 
    private Context context; 
    public static final String ACTION = "mycusomactionstring"; 
    private String preparedSql = ""; 
    private int id; 
    private String TABLE; 
    private String ID_FIELD; 
    private JSONObject OBJECT; 
    private String[][] KEYS_TYPES; 
    private SQLiteStatement stmt; 


    public DBDatabase(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     this.context = context; 
     this.db = this.getWritableDatabase(); 
    } 



    public void addAll(JSONArray ARRAY, String TABLE, String ID_FIELD, String[][] KEYS_TYPES) { 
     this.prepAdd(TABLE, ID_FIELD, KEYS_TYPES); 

     if(doesTableExist(db, TABLE)) { 
      try { 
       for (int i = 0; i < ARRAY.length(); i++) { 
        JSONObject obj = ARRAY.getJSONObject(i); 
        add(obj); 
       } 

       Intent in = new Intent(ACTION); 
       context.sendBroadcast(in); 

      } catch (JSONException e) { 
       Log.e("SQLite ERROR: " + e.toString()); 
      } 
     } else { 
      createTable(db, TABLE); 
      addAll(ARRAY, TABLE, ID_FIELD, KEYS_TYPES); 
     } 
    } 

    public void add(JSONObject obj) { 
     db.beginTransaction(); 

     try { 
      this.stmt= db.compileStatement(this.preparedSql); 
      stmt = bindValues(stmt, obj); 
      stmt.execute(); 
     } finally { 

     } 
     db.setTransactionSuccessful(); 
     db.endTransaction(); 
    } 



    public DBDatabase prepAdd(String TABLE, String ID_FIELD, String[][] KEYS_TYPES) { 
     this.db = this.getWritableDatabase(); 

     this.TABLE = TABLE; 
     this.ID_FIELD = ID_FIELD; 
     this.KEYS_TYPES = KEYS_TYPES; 

     prepareSql(); 

     return this; 
    } 



    public void prepareSql() { 

     this.preparedSql = "INSERT OR REPLACE INTO " + TABLE; 
     String keys = " ("; 
     String values = " VALUES("; 
     for (int i = 0; i < this.KEYS_TYPES.length; i++) { 
      String[] pair = this.KEYS_TYPES[i]; 
      String KEY = pair[0]; 
      if (this.KEYS_TYPES.length != i + 1) { 
       keys += KEY + ", "; 
       values += "?, "; 
      } else { 
       keys += KEY; 
       values += "?"; 
      } 
     } 
     keys += ")"; 
     values += ")"; 
     this.preparedSql += keys + values; 
} 

    private SQLiteStatement bindValues(SQLiteStatement statement, JSONObject obj) { 
     statement.clearBindings(); 
     try { 
      for(int i=0;i<this.KEYS_TYPES.length;i++) { 
      String[] pair = this.KEYS_TYPES[i]; 
      String KEY = pair[0]; 

      switch(pair[1]) { 
       case "int": 
        if(obj.has(KEY)) { 
         if(obj.isNull(KEY)) { 
          statement.bindLong(i + 1, 0); 
         } else { 
          statement.bindLong(i + 1, obj.getInt(KEY)); 
         } 
        } else { 
         statement.bindLong(i + 1, 0); 
        } 

        break; 
       case "string": 
        if(obj.has(KEY)) { 
         if (obj.isNull(KEY)) { 
          statement.bindString(i+1, ""); 
         } else { 
          statement.bindString(i+1, obj.getString(KEY)); 
         } 
        } else { 
         statement.bindString(i+1, ""); 
        } 
        break; 
      } 
     } 
    } catch (JSONException e) { 
     Log.e("HGDatabase ERROR", "PUT VALUES ERROR: " + e.toString()); 
    } 
    return statement; 
} 
+0

你能告訴你的add()函數的代碼?是否有任何工作正在另一個線程中完成? –

+0

@FrankD。編輯我的問題是更具體.. – EspeH

+0

我唯一能找到的是,你可以嘗試在try和finally塊中的add()函數中放入幾行代碼,如下例所示:http:// developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#beginTransaction() –

回答

1

嘗試在DBDatabase.add()函數的末尾添加廣播代碼。

public void add(String table, int key, String value){ 
    //whatever add() does currently 

    //make sure this is at the end of the function 
    Intent in = new Intent(ACTION); 
    LocalBroadcastManager.getInstance(context).sendBroadcast(in); 
} 
+0

這不會幫助我,仍然是同樣的結果。 – EspeH

0

自定義動作字符串必須包括包名稱,例如「de.example.com.MYACTION」。

一個較短的方式來發送一個廣播context.sendBroadcast(in)

+0

這不是我的問題的答案 – EspeH