Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6017721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:10:12+00:00 2026-05-23T03:10:12+00:00

I’m trying to query the data from the CallLog and insert in DB. For

  • 0

I’m trying to query the data from the CallLog and insert in DB. For that, I’ve created a COntentObserver as inner class in a Service, and inside onChange() method, I call my method that goes to the specified URI and query the data that has changed.

But, lets say, I received a call, so the observer was notified. So, my method goes to the call log content provider, query and insert, but it is inserting two, three times the same register.

Here is the code of my service.

public class RatedCallsService extends Service 

private Handler handler = new Handler();
    private SQLiteDatabase db;
    private OpenHelper helper;
    private String theDate;
    private String theMonth_;
    private String theYear_;
    private String theDay_;
    public static boolean servReg = false;

    class RatedCallsContentObserver extends ContentObserver {

            public RatedCallsContentObserver(Handler h) {

                super(h);
                //helper = new OpenHelper(getApplicationContext());
                //db = helper.getWritableDatabase();

            }

            @Override
            public boolean deliverSelfNotifications() {

                return true;

            }

            @Override
            public void onChange(boolean selfChange) {

                super.onChange(selfChange);
                Log.i(LOG_TAG, "Inside on Change. selfChange " + selfChange);
                searchInsert();
            }
        }

        @Override
        public IBinder onBind(Intent arg0) {

            return null;

        }

        @Override

        public void onCreate() {
            servReg = true;
            db = DataHandlerDB.createDB(this);
            registerContentObserver();

        }

        @Override
        public void onDestroy() {

            super.onDestroy();
            db.close();
            this.getApplicationContext().getContentResolver().unregisterContentObserver(new RatedCallsContentObserver(handler));

        }

        private void searchInsert() { 

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

    if (cursor.moveToFirst()) {

        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 numTypeId = cursor
                .getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
        int callTypeId = cursor
                .getColumnIndex(android.provider.CallLog.Calls.TYPE);

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

        SimpleDateFormat dateFormat = new SimpleDateFormat("M/d/yyyy");

        Date date = new Date();

        cursor.moveToFirst();

        String contactNumber = cursor.getString(numberColumnId);
        String contactName = (null == cursor.getString(contactNameId) ? ""
                : cursor.getString(contactNameId));
        String duration = cursor.getString(durationId);
        String numType = cursor.getString(numTypeId);
        String callType = cursor.getString(callTypeId);

        seconds = Integer.parseInt(duration);

        theDate = dateFormat.format(date);

        if (theDate.length() == 9) {

            theMonth_ = theDate.substring(0, 1);
            theDay_ = theDate.substring(2, 4);
            theYear_ = theDate.substring(5, 9);

        } else if (theDate.length() == 10) {

            theMonth_ = theDate.substring(0, 2);
            theDay_ = theDate.substring(3, 4);
            theYear_ = theDate.substring(6, 10);

        } else if (theDate.length() == 8) {

            theMonth_ = theDate.substring(0, 1);
            theDay_ = theDate.substring(2, 3);
            theYear_ = theDate.substring(4, 8);

        }

        ContentValues values = new ContentValues();
        ContentValues values2 = 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", Utilities.convertTime(seconds));
        values.put("date", dateFormat.format(date));
        values.put("current_time", currTime);
        values.put("cont", 1);
        values.put("type", callType);

        values2.put("month",
                Utilities.monthName(Integer.parseInt(theMonth_)));
        values2.put("duration", Utilities.convertTime(seconds));
        values2.put("year", theYear_);
        values2.put("month_num", Integer.parseInt(theMonth_));

        if (!db.isOpen()) {
            db = getApplicationContext()
                    .openOrCreateDatabase(
                            "/data/data/com.project.myapp/databases/myDb.db",
                            SQLiteDatabase.OPEN_READWRITE, null);
        }           
        if (duration != "") {               
            if (Integer.parseInt(duration) != 0) {

                String existingMonthDuration = DataHandlerDB
                        .selectMonthsDuration(theMonth_, theYear_, this);
                Integer newMonthDuration;


                if (existingMonthDuration != "") {

                    newMonthDuration = Integer
                            .parseInt(existingMonthDuration)
                            + Integer.parseInt(duration);                       

                    values2.put("duration",
                            Utilities.convertTime(newMonthDuration));
                    db.update(DataHandlerDB.TABLE_NAME_3, values2,
                            "year = ?", new String[] { theYear_ });

                } else {

                    db.insert(DataHandlerDB.TABLE_NAME_3, null, values2);

                }

                db.insert(DataHandlerDB.TABLE_NAME_2, null, values);

            }
        }
        cursor.close();

    }

            }

    public void registerContentObserver() {

            this.getApplicationContext()
                    .getContentResolver()
                    .registerContentObserver(
                            android.provider.CallLog.Calls.CONTENT_URI, false,
                            new RatedCallsContentObserver(handler));

        }
    }

I’ve tried everything. unregistering the observer, etc. but nothing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:10:12+00:00Added an answer on May 23, 2026 at 3:10 am

    I selected the timestamp of the call from android.provider.CallLog.Calls.DATE and before I insert I check if there is some timestamp like that one, if there is I dont insert, if there isnt I insert the data. This values are unique, so never will have some like each other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.