为什么ContentObserver被多次调用?

时间:2011-09-20 23:55:59

标签: android android-2.2-froyo contentobserver

我在接收和编写SMS时遵循ContentObserver实现,但它被多次调用。

代码:

public class SMSObserverActivity extends Activity {
    protected MyContentObserver observer = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String url = "content://mms-sms/";
        Uri uri = Uri.parse(url);
        observer = new MyContentObserver(new Handler());
        getContentResolver().registerContentObserver(uri, true, observer);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();

        getContentResolver().unregisterContentObserver(observer);
    }

    class MyContentObserver extends ContentObserver {
        ContentValues values = new ContentValues();
        Handler handler;

        public MyContentObserver(Handler handler){
            super(handler);
            this.handler = handler;
        }

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


        @Override
        public void onChange(boolean arg0){
            super.onChange(arg0);

            Log.v("SMS", "Notification on SMS observer");
            values.put("status", 5);
            Message msg = new Message();
            msg.obj = "xxxxxxxxxx";
            int threadId = 0;
            handler.sendMessage(msg);

            Uri uriSMSURI = Uri.parse("content://sms/");
            Cursor cur =
                    getContentResolver().query(uriSMSURI, null, null, null,
                            null);
            cur.moveToNext();
            Log.e("sms", cur.getString(4)+" "+cur.getString(11));
        }
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SMSObserverActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

为什么要多次调用?

编辑:
有人认为问题是由缺失的unregisterContentObserver造成的,但没有区别。

3 个答案:

答案 0 :(得分:11)

这是因为您正在为整个SMS数据库注册内容观察者。因此,每次数据库中的表条目更新时,您的内容观察者都会收到通知。

在这种情况下,例如,当发送一条消息时,大约有7个表条目会更新,因此您的内容观察者会收到7次通知。

因为我只对发送消息感兴趣,所以我改为只观察排队的消息,这意味着我的观察者总是会收到三次通知,所以我已经实现了代码以防止这种情况。

可能存在一些其他问题,例如多收件人或多部分邮件,但到目前为止基本工作仍然有效。

答案 1 :(得分:2)

为避免内容观察者发送多个短信,请尝试以下

public class SmsObserver extends ContentObserver {
    SharedPreferences trackMeData;
    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();

    }

答案 2 :(得分:0)

如果您希望仅在活动处于活动状态时启用观察者,我建议您分别将registerContentObserver()unregisterContentObserver()移至方法onResume()onPause() 。如果您的应用程序退出,则可能无法调用onDestroy(),但保证onPause()为。{/ p>