differentiate inbox and sentsms from all sms

后端 未结 1 1112
悲&欢浪女
悲&欢浪女 2021-01-07 12:07

I am working on an ANdroid application.In my app I have to list all the conversations and i did that part.Each conversation is containing all sms to that number. So i have t

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 13:13

    You can use ContentObserver here to track sent and received message,

    Override onChange() method of ContentObserver and get the sms type and work accordingly. Psuedo code can be as below.

    Cursor cursor = mContext.getContentResolver().query(Uri
                                 .parse("content://sms"), null, null, null, null);
    
    String type = cursor.getColumnIndex("type");
    if(cursor.getString(type).equalsIgnoreCase("1")){
        // sms received
     }
     else if(cursor.getString(type).equalsIgnoreCase("2")){
        //sms sent
     }
    

    Registering ContentObserver for SMS,

    ContentResolver observer = this.getContentResolver();
    observer.registerContentObserver(Uri.parse("content://sms"), 
                                   true, new MySMSObserver(new Handler(),this));
    

    Where MySMSObserver will be your class extending ContentObserver with Constructor having argument as Handler and Context,

    public MySMSObserver(Handler handler, Context context) {
            super(handler);
            this.context = context;
    }
    

    0 讨论(0)
提交回复
热议问题