JonSkeet 2013-05-24 09:41 采纳率: 0%
浏览 2138

区分 sms 中的 inbox 和 sentsms

我创建了一个android程序。在程序中需要列出所有的会话。每个会话包含所有的sms号码。因此我需要区分sms中的inbox和sentsms。
下面的代码用于寻找inbox和sent:

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

但是我不想使用这个方法,我通过使用api列出所有的sms

content://sms/

我使用 columnindex的 type和address 来测试。但是总是给出inbox 和 outbox相同的结果。

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

请问如何区分 sms 中的 inbox 和 sentsms?

  • 写回答

1条回答 默认 最新

  • LiDaming_and 2013-05-27 02:03
    关注

    你可以使用 ContentObserver 来追踪发送和接收信息。
    重写 ContentObserver 中的 onChange()方法,获得响应的 sms 类型:

    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
     }
     else if(cursor.getString(type).equalsIgnoreCase("2")){
        //发送 sms 
     }
    

    给 SMS 登记 ContentObserver

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

    MySMSObserver 会使用 Constructor 继承 ContentObserver,Handler and Context作为参数。

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器