SmS计数没有增加。消息ID是常量

时间:2012-04-18 04:37:28

标签: android sms

这里我每次收到或发送短信时都会向DB插入一条记录。它的工作正常。但有一段时间所有的短信都会得到相同的ID。在哪种情况下,我为不同的短信获得相同的短信ID。

    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);

        }

这是没有插入表的sms的logcat内容。但是在我们的情况下,当第一个发送者第二次发送sms时,新到达的消息id没有分配给变量message_id所以两个变量{{1 }和message_id将具有相同的值,因此消息计数不会递增。

previousmsg_id

仅在HTC设备中发生这种情况。在其他设备中它的工作正常。那么我该如何解决这个问题呢。谢谢你的回答。

1 个答案:

答案 0 :(得分:1)

如果您在Android手机中计算传入和传出的Sms,则使用此代码:

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();

    }

}

}