Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I want to implement a program which inserts details in database everytime the call is made or sms is sent. Should I use a broadcast receiver or content observer or Service ? what would be appropriate? I am new to android and urgently need help on this.


I have done the following code uptill now. The problem with this is when first time the code is run then for example call log has 8 records so these 8 records are inserted in database. Then if any change in call log is made for example i make another call then 17 records are shown in database instead of 9. Please help where am i going wrong ?

Java
    package com.calllogdb;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;




import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

//import static android.provider.BaseColumns._ID;
import static com.calllogdb.Constants.KEY_ContactNum ;
import static com.calllogdb.Constants.KEY_ContactName;
import static com.calllogdb.Constants.KEY_Duration;
import static com.calllogdb.Constants.KEY_Date ;
import static com.calllogdb.Constants.KEY_NumType ;
import static com.calllogdb.Constants.KEY_CurrTime ;
import static com.calllogdb.Constants.TABLE_NAME;

public class MainActivity extends Activity {

    private Helper helper = new Helper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          this.getApplicationContext()
            .getContentResolver()
            .registerContentObserver(
                    android.provider.CallLog.Calls.CONTENT_URI, true,
                    new CallLogObserver(new Handler()));



        try {

            addLog();
        }
    finally {

        helper.close();
    }
    }

    private void addLog() {


        // TODO Auto-generated method stub
        SQLiteDatabase db;

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

    db = helper.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(KEY_ContactName, contactName);
       values.put(KEY_NumType, numType);
            values.put(KEY_ContactNum, contactNumber);
            values.put(KEY_Duration, duration);
            values.put(KEY_Date, callDate);
            values.put(KEY_CurrTime, currTime);

            db.insert(TABLE_NAME, null, values);


            callList.add("Contact Number: " + contactNumber
                    + "\nContact Name: " + contactName + "\nDuration: "
                    + duration + "\nDate: " + callDate);


        } while (cursor.moveToNext());
}
    Toast.makeText(getBaseContext(), "Inserted!", Toast.LENGTH_LONG).show();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }




    public class CallLogObserver extends ContentObserver
    {



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


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

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

            }





    }
}


Whenever i am making a call it gets notified and inserts whole set of records instead of only the latest one.What should i do to prevent this?
Thanks
Posted
Updated 23-Jul-13 6:27am
v2

1 solution

check this link
databse-operations-in-sqlite[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900