区分所有短信的收件箱和sentsms

sar*_*ath 1 android android-contentprovider

我正在研究一个ANdroid应用程序.在我的应用程序中,我必须列出所有的对话,我做了那个部分.每个对话都包含该号码的所有短信.因此我必须对所有短信的收件箱和sentsms进行鉴别.我知道以下api可用于查找收件箱并发送.

content://sms/inbox
content://sms/sent
Run Code Online (Sandbox Code Playgroud)

但我不想使用它.我使用api列出了所有的短信

content://sms/
Run Code Online (Sandbox Code Playgroud)

我测试了columnindex的类型,地址,但它总是给收件箱和发件箱提供相同的结果.我的示例代码是

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();
Run Code Online (Sandbox Code Playgroud)

请帮我.

Lal*_*ani 6

您可以在此处使用ContentObserver来跟踪发送和接收的消息,

覆盖onChange()方法ContentObserver并获取短信类型并相应地工作.Psuedo代码可以如下.

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
 }
Run Code Online (Sandbox Code Playgroud)

注册ContentObserver短信,

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

MySMSObserver将你的类扩展ContentObserver具有参数作为处理程序和上下文构造,

public MySMSObserver(Handler handler, Context context) {
        super(handler);
        this.context = context;
}
Run Code Online (Sandbox Code Playgroud)

  • 因为ContentObserver会观察您的收件箱更改.每次收到或发送短信时,都会调用它的`onChange()`. (2认同)