differentiate inbox and sentsms from all sms

℡╲_俬逩灬. 提交于 2019-12-19 04:56:33

问题


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 to differtiate inbox and sentsms from all sms.I know about the following api's can use to find inbox and sent.

content://sms/inbox
content://sms/sent

But i don't want to use this.I listed all sms by using the api

content://sms/

I tested with the columnindex's type,address but it always gives the same result for inbox and outbox.And my sample code is

Uri SMS_INBOX = Uri.parse("content://sms");
        c = getContentResolver().query(SMS_INBOX, null, "thread_id" + " = "
                        + "3", null,
                        "date" + " ASC");
        if(c.moveToFirst()){
            count.add(c.getCount());
            for(int j=0;j<c.getCount();j++){
                System.out.println(c.getString(c.getColumnIndexOrThrow("body")).toString());
                System.out.println("new   person=="+c.getColumnIndex("person")+"type=="+c.getColumnIndexOrThrow("type"));
                c.moveToNext();
            }
        }
        c.close();

Please help me.


回答1:


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


来源:https://stackoverflow.com/questions/11980788/differentiate-inbox-and-sentsms-from-all-sms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!