28

I'd like to know if there is a way to know if the content provider of callings has changed. I mean, if I make a call, or I answer a call, it returns a "flag" that a new log has been added to the call log, or the place where Android store informations about callings.

Because, when I make a call, Android stores the number, the contact name (if exists), the hour of the calling, the duration, ..., all in the content provider. So is there a way to capture this "flag" that says the content provider of callings is bigger, I mean, that a new data has been inserted on the content provider CallLog.Calls.

So, I still have a lot of doubts related to this issue. I don't know where to register the content observer. My intention is, when something changes in the CallLog content provider, the insert method of the code will be used.

I mean, the code won't do anything unless new data has been added to the CallLog content provider. If some data has been added to the CallLog content provider, then the code will query the new data, and then will insert. I wanna do this, because without a content observer the application was inserting data in the database that was already inserted every time I run the application, got it?

So here is my code. If someone could tell me where to put registerContentObserver() and everything else that is needed.

public class RatedCalls extends ListActivity {

    private SQLiteDatabase db;
    private CallDataHelper dh = null;
    StringBuilder sb = new StringBuilder();
    OpenHelper openHelper = new OpenHelper(RatedCalls.this);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor cursor = getContentResolver().query(
                android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
                android.provider.CallLog.Calls.DATE + " DESC ");

        dh = new CallDataHelper(this);
        db = openHelper.getWritableDatabase();

        startManagingCursor(cursor);
        int numberColumnId = cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
        int durationId = cursor.getColumnIndex(android.provider.CallLog.Calls.DURATION);
        int contactNameId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
        int dateId = cursor.getColumnIndex(android.provider.CallLog.Calls.DATE);
        int numTypeId = cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);

        Date dt = new Date();
        int hours = dt.getHours();
        int minutes = dt.getMinutes();
        int seconds = dt.getSeconds();
        String currTime = hours + ":" + minutes + ":" + seconds;

        ArrayList<String> callList = new ArrayList<String>();
        if (cursor.moveToFirst()) {
            do {
                String contactNumber = cursor.getString(numberColumnId);
                String contactName = cursor.getString(contactNameId);
                String duration = cursor.getString(durationId);
                String callDate = DateFormat.getDateInstance().format(dateId);
                String numType = cursor.getString(numTypeId);

                ContentValues values = new ContentValues();
                values.put("contact_id", 1);
                values.put("contact_name", contactName);
                values.put("number_type", numType);
                values.put("contact_number", contactNumber);
                values.put("duration", duration);
                values.put("date", callDate);
                values.put("current_time", currTime);
                values.put("cont", 1);

                this.db.insert(CallDataHelper.TABLE_NAME, null, values);

                Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG);
                callList.add("Contact Number: " + contactNumber
                        + "\nContact Name: " + contactName + "\nDuration: "
                        + duration + "\nDate: " + callDate);

            } while (cursor.moveToNext());
        }
        setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, callList));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
JJD
  • 50,076
  • 60
  • 203
  • 339
rogcg
  • 10,451
  • 20
  • 91
  • 133

2 Answers2

43

Here is the answer. Dont forget to register the content observer with this method:

registerContentObserver (Uri uri, boolean notifyForDescendents, ContentObserver observer)

And then you can create it like this.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getApplicationContext()
    .getContentResolver()
    .registerContentObserver(
            android.provider.CallLog.Calls.CONTENT_URI, true,
            new MyContentObserver(handler)); 
}

class MyContentObserver extends ContentObserver {
    public MyContentObserver(Handler h) {
        super(h);
    }

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

    @Override
    public void onChange(boolean selfChange) {
        Log.d(LOG_TAG, "MyContentObserver.onChange("+selfChange+")");
        super.onChange(selfChange);

        // here you call the method to fill the list
    }
}
rogcg
  • 10,451
  • 20
  • 91
  • 133
  • 8
    don't forget to unregister the provider in the onDestroy() method. – moonlightcheese Mar 11 '11 at 15:35
  • what happens if you don't? i find that in order to unregister the observer, i have to keep it in memory, which i'd rather not do. – Ben H Apr 05 '11 at 06:35
  • 4
    in the line new RatedCallsContentObserver(handler)); where does the handler come from / why do I need this? thanks! – stefan.at.kotlin May 31 '11 at 17:14
  • 3
    just passing in new Handler() instead of handler works. I am just not sure if it's the best way, have to read on it. – stefan.at.kotlin May 31 '11 at 17:20
  • 1
    this handler parameter is to run onChange(boolean) on. onChange() will happen on the provider Handler. See the docs http://developer.android.com/reference/android/database/ContentObserver.html#ContentObserver%28android.os.Handler%29 – rogcg Jun 02 '11 at 13:48
  • I override the method deliverSelfNotifications() still it gets called multiple times. – Lalit Poptani Mar 10 '12 at 07:51
  • Thanks for the great answer! But what is the role of the Toast by `Toast.makeText(getBaseContext(), "Didn't called onChange()", Toast.LENGTH_LONG);` ? – Vincent Vettukal Jul 18 '12 at 08:06
  • @VincentVettukal that was just a test to know if it was calling the `onChange()` method or not. It doesn't have any influence in the code. – rogcg Jul 20 '12 at 13:52
  • 2
    @BenH If you don't unregister a previously registered observer, it *will* stay in memory. You will get a memory leak because the object that you registered to keeps a reference to your activity. – Maarten Oct 22 '13 at 17:34
  • @rogcg inside onChange() you are getting latest call information ? I have some doubts please help if you can : http://stackoverflow.com/questions/27024342/how-can-we-hide-call-logs-of-specific-contact-in-android – Hardik Joshi Nov 19 '14 at 18:53
  • Why would I want to unregister it if I'm running a background service that monitors the call log? – Ronen Festinger Nov 26 '16 at 04:45
1

Simply create a new subclass of ContentObserver class and override it's onChange() method. The onChange() method will contain all the code that will be executed on content change.

public class MyObserver extends ContentObserver {
    public MyObserver(Handler handler) {
            super(handler);
        }

    @Override
    public void onChange(boolean selfChange) {
        this.onChange(selfChange,null);
    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {
        //Write your code here      }
}

Then all you have to do is register your content provider to the URI.

getContentResolver().registerContentObserver(YourURI,true,myObserver);

Remember to unregister your content observer object or else it can lead to memory leak.

Source: Use Android's ContentObserver To React On Content Changes