10

我正在为content://sms. 我正在将所有消息写入 SD 卡中的文本文件。但是onChange()内容观察器中的方法被多次调用,并且相同的消息被多次写入文本文件。如何避免这种情况?另外我想知道内容观察器是否会减慢电话速度。

4

4 回答 4

7

您需要覆盖 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");
}
}
于 2011-06-07T06:32:41.837 回答
1

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

于 2011-05-23T09:48:15.687 回答
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();

    }
于 2013-12-25T07:50:08.667 回答
0

onChange() 事件为每列发生变化(即“正文”或“地址”)调用一次——因此是多次调用。只处理一次数据的最简单方法是存储“_id”列的值,查看它是否在下一次迭代中发生了变化,然后忽略。

cur.getColumnIndex("_id")

请参阅此处的示例: Sms ContentObserver onChange() 多次触发

于 2013-09-11T21:21:42.140 回答