Android:内容观察者的“onChange()”方法被多次调用

时间:2011-05-17 05:22:15

标签: android contentobserver

我正在使用content://sms的内容观察者。我正在将所有消息写入SD卡中的文本文件。但是内容观察器中的onChange()方法被多次调用,并且相同的消息被多次写入文本文件。怎么避免这个?此外,我想知道是否让内容观察者放慢手机速度。

4 个答案:

答案 0 :(得分:6)

您需要覆盖deliverSelfNotifications()以返回true。

class ObserverSms extends ContentObserver {
private Context mContext;

public ObserverSms(Context context, Handler handler) {
    super(handler);
    mContext = context;
}

@Override
public boolean deliverSelfNotifications() {
    return true;
}

@Override
public void onChange(boolean selfChange) {
    super.onChange(selfChange);
    MyLog.logDebugInConsole(TAG, "Sms Database Changed");
}
}

答案 1 :(得分:1)

Vivek,请确保在您离开活动时取消注册您的内容观察员,例如在onDestroy()方法中,请致电unregisterContentObserver()。希望这有帮助! (在我的情况下,它有效)

答案 2 :(得分:1)

尝试这样做以避免多次执行onChange()

private Context context;
    private static int initialPos;
    private static final String TAG = "SMSContentObserver";
    private static final Uri uriSMS = Uri.parse("content://sms/sent");

    public SmsObserver(Handler handler, Context ctx) {
        super(handler);
        context = ctx;
        trackMeData = context.getSharedPreferences("LockedSIM", 0);
        initialPos = getLastMsgId();

    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        queryLastSentSMS();
    }

    public int getLastMsgId() {

        Cursor cur = context.getContentResolver().query(uriSMS, null, null, null, null);
        cur.moveToFirst();
        int lastMsgId = cur.getInt(cur.getColumnIndex("_id"));
        Log.i(TAG, "Last sent message id: " + String.valueOf(lastMsgId));
        return lastMsgId;
    }

    protected void queryLastSentSMS() {

        new Thread(new Runnable() {

            @Override
            public void run() {
                Cursor cur =
                    context.getContentResolver().query(uriSMS, null, null, null, null);

                if (cur.moveToNext()) {



                    try {

                        String body = cur.getString(cur.getColumnIndex("body"));

                        if (initialPos != getLastMsgId()) {

                            String receiver = cur.getString(cur.getColumnIndex("address"));
                            Log.i("account", myDeviceId);
                            Log.i("date", day + "-" + month + "-" + year + " "
                                + hour + ":" + minute + ":" + seconde);
                            Log.i("sender", myTelephoneNumber);
                            Log.i("receiver", receiver );


                            // Then, set initialPos to the current position.
                            initialPos = getLastMsgId();

                            sendsmstoph(receiver, body);
                        }
                    } catch (Exception e) {
                        // Treat exception here
                    }
                }
                cur.close();
            }
        }).start();

    }

答案 3 :(得分:0)

为每个更改的列(即“body”或“address”)调用onChange()事件一次 - 因此多次调用。仅处理数据一次的最简单方法是存储“_id”列的值,看它是否在下一次迭代时发生了变化,然后忽略。

cur.getColumnIndex("_id")

见这里的例子: Sms ContentObserver onChange() fires multiple times