ContentObserver on content://sms/ in 1.6+?

北城以北 提交于 2020-01-01 10:13:56

问题


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

    }

}

回答1:


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

Please ensure the process is running for monitoring the changes.

My device is milestone (2.1 update1)



来源:https://stackoverflow.com/questions/2591530/contentobserver-on-content-sms-in-1-6

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