为什么ContentObserver多次调用?ContentObserver

2023-09-06 02:35:10 作者:掏粪男孩该吃药i

我有以下 ContentObserver 实施接收,写短信,但它被称为多次。

I have following ContentObserver implementation for receiving and writing SMS, but it is called multiple times.

code:

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>

为什么多次调用?

Why is it called multiple times?

编辑: 有这个想法,这个问题是由于缺少 unregisterContentObserver ,但它没有什么区别。

There was the idea that the problem is caused by the missing unregisterContentObserver, but it makes no difference.

推荐答案

这是存在的,因为你是注册内容观察员整个SMS数据库。所以,你的内容观察者得到通知,每次在数据库中的表项被更新。

This is occurring because you are registering your content observer for the entire SMS database. So your content observer gets notified each time a table entry in the database gets updated.

在当邮件被发送,例如约7表中的条目得到更新,以便您的内容观察者得到通知7次这样的情况。

In this case when a message is sent for example around 7 tables entries get updated so your content observer gets notified 7 times.

由于我只有一条消息被发送我已经改变了,只看到排队的消息,这意味着我的观察总是被告知确切的三倍,所以我已经实现了code,以防止有兴趣的。

Since I'm only interested if a message is sent I've changed to only observe the queued messages and this means my observer always gets notified exactly three times so I have implemented code to protect against that.

有可能是一些其他的问题,如多收件人或多个部分组成的消息,但基础工作至今。

There are likely to be some other issues such as multi recipient or multi part messages but the basics work so far.