1.6+ 中 content://sms/ 上的 ContentObserver?

发布于 2024-08-28 03:33:34 字数 2210 浏览 15 评论 0原文

我有一个内容观察器,它会在 android 1.5 中轮询 content://sms/ ,以便我收到短信数据库中的更改通知,并可以相应地做出反应。

然而在 1.6 中这不起作用,uri 是否从 content://sms/ 更改为其他内容?

我在 1.6 设备上的 logcat 中看到了 content://mms-sms/ 弹出,但我已经尝试过了,但它不起作用。

这是我的代码

String url = "content://sms/"; 
        Uri uri = Uri.parse(url); 
        getContentResolver().registerContentObserver(uri, true, new MyContentObserver(handler)); 


}

class MyContentObserver extends ContentObserver { 

    public MyContentObserver(Handler handler) { 

        super(handler); 

    }

@Override public boolean deliverSelfNotifications() { 
    return false; 
    }

ContentValues values = new ContentValues();


@Override public void onChange(boolean arg0) { 
    super.onChange(arg0);

     Log.v("SMS", "Notification on SMS observer"); 
     values.put("status", 5);
    Message msg = new Message(); 
    msg.obj = "xxxxxxxxxx";
    int threadId = 0;
    handler.sendMessage(msg);

    Uri uriSMSURI = Uri.parse("content://sms/");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null,
                 null, null);
    cur.moveToNext();
    String protocol = cur.getString(cur.getColumnIndex("protocol"));
    Log.d("SMS", "SMS PROTOCOL = " + protocol); 

    if(protocol == null){
           Log.d("SMS", "SMS SEND"); 
           threadId = cur.getInt(cur.getColumnIndex("thread_id"));
           int status = cur.getInt(cur.getColumnIndex("status"));
           Log.d("SMS", "STATUS = " + status);


           if(status != 5){
           Uri updateUri = ContentUris.withAppendedId(Uri.parse("content://sms/conversations/"), threadId);
           int rows = getContentResolver().update(updateUri, values, null, null);
           Log.d("SMS", "ROWS UPDATED = " + rows);
           Log.d("SMS 2", "STATUS = " + status);
           }


           Log.d("SMS", "SMS SEND ID = " + threadId); 

           String textBody = cur.getString(cur.getColumnIndex("body"));
           String textAddress  = cur.getString(cur.getColumnIndex("address"));
           Log.d("SMS", "SMS SEND ADDRESS= " + textAddress); 
           Log.d("SMS", "SMS SEND BODY= " + textBody); 


    }
    else{
        Log.d("SMS", "SMS RECIEVE");  

    }

}

I have a content observer that polls content://sms/ in android 1.5 so that I get notified of changes in the sms database and can react to them accordingly.

However in 1.6 this doesn't work, has the uri been changed from content://sms/ to something else?

I have seen content://mms-sms/ popping up in the logcat on my 1.6 device but I have tried that and it doesn't work.

Here is my code

String url = "content://sms/"; 
        Uri uri = Uri.parse(url); 
        getContentResolver().registerContentObserver(uri, true, new MyContentObserver(handler)); 


}

class MyContentObserver extends ContentObserver { 

    public MyContentObserver(Handler handler) { 

        super(handler); 

    }

@Override public boolean deliverSelfNotifications() { 
    return false; 
    }

ContentValues values = new ContentValues();


@Override public void onChange(boolean arg0) { 
    super.onChange(arg0);

     Log.v("SMS", "Notification on SMS observer"); 
     values.put("status", 5);
    Message msg = new Message(); 
    msg.obj = "xxxxxxxxxx";
    int threadId = 0;
    handler.sendMessage(msg);

    Uri uriSMSURI = Uri.parse("content://sms/");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null,
                 null, null);
    cur.moveToNext();
    String protocol = cur.getString(cur.getColumnIndex("protocol"));
    Log.d("SMS", "SMS PROTOCOL = " + protocol); 

    if(protocol == null){
           Log.d("SMS", "SMS SEND"); 
           threadId = cur.getInt(cur.getColumnIndex("thread_id"));
           int status = cur.getInt(cur.getColumnIndex("status"));
           Log.d("SMS", "STATUS = " + status);


           if(status != 5){
           Uri updateUri = ContentUris.withAppendedId(Uri.parse("content://sms/conversations/"), threadId);
           int rows = getContentResolver().update(updateUri, values, null, null);
           Log.d("SMS", "ROWS UPDATED = " + rows);
           Log.d("SMS 2", "STATUS = " + status);
           }


           Log.d("SMS", "SMS SEND ID = " + threadId); 

           String textBody = cur.getString(cur.getColumnIndex("body"));
           String textAddress  = cur.getString(cur.getColumnIndex("address"));
           Log.d("SMS", "SMS SEND ADDRESS= " + textAddress); 
           Log.d("SMS", "SMS SEND BODY= " + textBody); 


    }
    else{
        Log.d("SMS", "SMS RECIEVE");  

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不爱素颜 2024-09-04 03:33:34

Uri.parse("content://mms-sms")

请确保该进程正在运行以监视更改。

我的设备是里程碑(2.1 update1)

Uri.parse("content://mms-sms")

Please ensure the process is running for monitoring the changes.

My device is milestone (2.1 update1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文