如何在sqlite插入/更新完成后触发sendBroadcast?

时间:2015-08-24 15:13:08

标签: android sqlite android-intent broadcastreceiver localbroadcastmanager

如何在sqlite插入/更新完成后触发sendBroadcast? BroadCastReceiver得到正确的广播。

我就是这样做的,但是在插入/更新完成之前发送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;
}

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

自定义操作字符串必须包含包名称,例如“de.example.com.MYACTION”。

发送广播的较短方式是context.sendBroadcast(in)