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 8451825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:20:37+00:00 2026-06-10T11:20:37+00:00

here I am inserting a record to DB each time I receive or send

  • 0

here I am inserting a record to DB each time I receive or send a SMS. its working fine.But some time all sms will get same id . In which situation do I get same sms id for different sms.

    public SmsMms(Handler handler) {
        super(handler);
        Log.d(TAG, "SMS Constructor");
    }

    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Log.d("sms", "SMS ONCHANGE");
        if(rc == null )
        rc = new RecordCount(getApplicationContext());


        Uri uriSMSURI = Uri.parse(SMS);
        Cursor cur = getContentResolver().query(uriSMSURI, null, null,
                null, null);
        int rCount = cur.getCount();
        long recCount = rc.select("SMS");

        Log.d("sms", "rCount from sms db: " + rCount + "reccout from device db: " + recCount);
        long diffCount;
        if(rCount > recCount){
            diffCount = rCount - recCount;
        }else {
            diffCount = recCount - rCount;
        }
        Log.d("sms", "diff: " + diffCount);
        Log.d("sms", "sms count: " + rc.select("SMS") + "===rCount: " + rCount);
        if (rCount >= recCount || diffCount > 1) {
            Log.d("sms", "diff: ");
            rc.updateRecordCount("SMS", rCount);
            cur.moveToNext();
            String protocol = cur.getString(cur.getColumnIndex("protocol"));
            String content = cur.getString(cur.getColumnIndex("body"));
            int msg_id = cur.getInt(cur.getColumnIndex("_id"));
            Log.d("sms", "Message_id: " + msg_id);

            if (protocol == null) {
                Log.d("timest", "check protocol");
                if (!content.equals(null) && msg_id != prev_msgid) {
                    Log.d("timest", "current msg" +msg_id);
                    dh.sms_insert(timeStamp(), content.length(), "sent");
                    prev_msgid = msg_id;
                    Log.d("timest", "previous msg"+prev_msgid);
                    Log.d("timest", "Outgoing Message" + content.length());

                }
            } else {
                Log.d("sms", "in else");
                Log.d("sms", "previous msg"+prev_msgid);
                Log.d("sms", "current msg id " +msg_id);
                if (!content.equals(null) && msg_id != prev_msgid ) {
                    Log.d("sms", "diff: " + diffCount);
                    Log.d("sms", "current msg id " +msg_id);
                    dh.sms_insert(timeStamp(), content.length(), "received");
                    prev_msgid = msg_id;
                    Log.d("sms", "Incoming Message" + content.length());

                }
            }
            dh.smsList();
            Log.d("sms", "msg list" + dh.smsList());
        }else if(rCount < recCount){
            rc.updateRecordCount("SMS", rCount);

        }

this is logcat content for sms not inserted to the table.But in our case when first sender sends sms for the second time the new arrived message id is not assigning to the variable message_idso both variables message_id and previousmsg_id will have the same value and hence message count is not incrementing.

04-18 09:39:36.092: D/sms(10033): SMS ONCHANGE
04-18 09:39:36.132: D/sms(10033): rCount from sms db: 52reccout from device db: 51
04-18 09:39:36.132: D/sms(10033): diff: 1
04-18 09:39:36.142: D/sms(10033): sms count: 51===rCount: 52
04-18 09:39:36.142: D/sms(10033): diff: 
04-18 09:39:36.142: D/SmsReceiverService(714): insertUri> content://sms/178
04-18 09:39:36.152: D/SmsReceiverService(714): class: UNKNOWN
04-18 09:39:36.182: D/MmsSmsProvider(307): ids: 11
04-18 09:39:36.182: D/MmsSmsProvider(307): recipientIds: 11
04-18 09:39:36.232: D/sms(10033): Message_id: 175
04-18 09:39:36.232: D/sms(10033): in else
04-18 09:39:36.232: D/sms(10033): previous msg175
04-18 09:39:36.232: D/sms(10033): current msg id 175
04-18 09:39:36.242: D/sms(10033): msg list[12-04-18 09:35:30 , 8,received
04-18 09:39:36.242: D/sms(10033): , 12-04-18 09:35:54 , 8,received
04-18 09:39:36.242: D/sms(10033): , 12-04-18 09:36:44 , 8,received
04-18 09:39:36.242: D/sms(10033): ]

This is happening only in HTC device. In other devices its working fine. so how can I solve this issue. thanks for answers.

  • 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-06-10T11:20:39+00:00Added an answer on June 10, 2026 at 11:20 am

    if you are counting incoming and outgoing Sms in android phone then used this code:

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
                Log.v("SMSTEST", "STARTED LISTENING FOR SMS OUTGOING");
                Handler handle = new Handler(){};
                SMSObserver myObserver = new SMSObserver(handle);
                ContentResolver contentResolver = getContentResolver();
                contentResolver.registerContentObserver(Uri.parse("content://sms"),true, myObserver);
    
    
    
    }
    
    
    private class SMSObserver extends ContentObserver{
    
          String lastMessage = null;
    
        public SMSObserver(Handler handler) {
            super(handler);
        }
    
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            db=new MessageDatabase(getApplicationContext());
            Uri uriSMSURI = Uri.parse("content://sms/");
            Cursor cur = getContentResolver().query(uriSMSURI, null, null,null, null);
            cur.moveToNext();
            String id = cur.getString(cur.getColumnIndex("thread_id"));        
            String protocol = cur.getString(cur.getColumnIndex("protocol"));
            int type = cur.getInt(cur.getColumnIndex("type"));
    
            if(protocol==null && type==2 )
            {
               //outflag=true;
                Log.i("SMSTEST", "SMS Send count: " + outbox);
                Cursor c = getContentResolver().query(Uri.parse("content://sms/outbox/" + id), null, null, null, null);
                if (c.getCount()>0){
                    if(c.moveToFirst()){
                    do{ 
                        System.out.println(" main cursor Value:"+c.getCount());
                        String name=cur.getString(cur.getColumnIndex("person"));               
                        String address=cur.getString(cur.getColumnIndex("address"));
                        System.out.println("\n name:"+name);                
                        System.out.println("\n address:"+address);
                        Cursor getting=db.getRecord(address);
                        if(getting.getCount()!=0)
                        {
                                boolean chck = getting.moveToFirst();   
                                System.out.println("Record found");
                                int cnt=getting.getInt(getting.getColumnIndex(db.SENDING));
                                int temp=cnt+1;
                                System.out.println("counter:"+temp);
                                db.updateoutboxCounter(temp, address);
    
                        }else{
                            System.out.println("Record not found");
                            db.insertRecord(address,1,0);
                        }
                        getting.close();
                    }while(c.moveToNext());
                    }
               }
    
                c.close();  
    
            Log.i("======TEST====", "MESSAGE SENT.......");
            }else if(protocol!=null && type==1)
            {
                Cursor c = getContentResolver().query(Uri.parse("content://sms/inbox/" + id), null, null, null, null);
                if (c.getCount()>0){
                    if(c.moveToFirst()){
                    do{ 
                        System.out.println(" main cursor Value:"+c.getCount());
                        String name=cur.getString(cur.getColumnIndex("person"));               
                        String address=cur.getString(cur.getColumnIndex("address"));
                        System.out.println("\n name:"+name);                
                        System.out.println("\n address:"+address);
                        Cursor getting=db.getRecord(address);
                        if(getting.getCount()!=0)
                        {
                                boolean chck = getting.moveToFirst();   
                                System.out.println("Record found");
                                int cnt=getting.getInt(getting.getColumnIndex(db.RECEVING));
                                int temp=cnt+1;
                                System.out.println("counter:"+temp);
                                db.updateinboxCounter(temp, address);
    
                        }else{
                            System.out.println("Record not found");
                            db.insertRecord(address,0,1);
                        }
                        getting.close();
                    }while(c.moveToNext());
                    }
               }
    
                c.close();
    
                Log.i("======TEST====", "MESSAGE RECEIVE.......");
            }
            cur.close();
    
        }
    
    }
    

    }

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

Sidebar

Related Questions

I am inserting a record into my table, but I want to be able
i have created table using Sqlite3.and also inserted record in that table.its running successfully.but
When inserting values into my table what do I edit here? Do I delete
I want to decode Unicode strings to UTF-8 when inserting in a table. Here's
Afetr inserting a new record into my CoreData store using an NSFetchedResultsController, when I
Created new scaffold Users that has fields name & description All working as suppose
What are the better techniques for inserting a record into a table if it
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I am inserting a record every hour into my table. Now I need to
I'm having a trouble with getting the id after inserting a new Record using

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.