【问题标题】:How can I trigger sendBroadcast after sqlite insert/update finished?sqlite插入/更新完成后如何触发sendBroadcast?
【发布时间】:2015-08-24 15:13:07
【问题描述】:

如何在 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;
}

【问题讨论】:

  • 您能展示一下您的 add() 函数的代码吗?是否有任何工作正在另一个线程中完成?
  • @FrankD。编辑我的问题更具体..
  • 我唯一能找到的是,你可以尝试在你的 add() 函数中的 try 和 finally 块中放入几行代码,就像在这个例子中:developer.android.com/reference/android/database/sqlite/…

标签: android sqlite android-intent broadcastreceiver localbroadcastmanager


【解决方案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);
}

【讨论】:

  • 这对我没有帮助,结果还是一样。
【解决方案2】:

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

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

【讨论】:

  • 这不是我的问题的答案
猜你喜欢
  • 2019-04-01
  • 2019-11-01
  • 2021-03-08
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
相关资源
最近更新 更多