Hi guys:

take a look at the following code:
--------------------------------------------------------------------------------------------------------------------------------
package com.demo.android.smsflowmeter;

import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class SMSFlowmeter extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                setReceiver();
        }

        private SmsSentCounter smsSentObserver = new SmsSentCounter(new
Handler());
        private int sms_sent_counter = 0;

        private void setReceiver() {
                this.getContentResolver().registerContentObserver(
                                Uri.parse("content://sms"), true, 
smsSentObserver);
        }

        // wait to be implemented
        class SmsSentCounter extends ContentObserver {

                public SmsSentCounter(Handler handler) {
                        super(handler);
                        // TODO Auto-generated constructor stub
                }

                @Override
                public void onChange(boolean selfChange) {
                        // TODO Auto-generated method stub
                        super.onChange(selfChange);
                        Cursor sms_sent_cursor = 
SMSFlowmeter.this.managedQuery(Uri
                                        .parse("content://sms"), null, "type=?",
                                        new String[] { "2" }, null);
                        if (sms_sent_cursor != null) {
                                if (sms_sent_cursor.moveToFirst()) {
                                        sms_sent_counter++;
                                        Log.d("test", "" + sms_sent_counter);
                                }
                        }
                }
        }
}

--------------------------------------------------------------------------------------------------------------------------------

the code is pretty simple, but it really confused me a lot. since
theres just one Log.d() in the snippet and onChange() is invoked only
if changes occur in the uri "content://sms", so i think when i send an
sms, onChange() should be invoked only once. but when i run this app
on the emulator and tried to send a message from the emulator, the
Log.d() output twice most of the time, and only when the first sms was
sent or when i deleted all the sms and then sent an sms again, it may
output just once. thiss really strange i couldnt figure it out. is
this a bug or its just my fault or sth else? any clues and ideas are
welcome! Cheers, Lloyd.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to