Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /var/www/since2006.com/public_html/include/util/Ubb.php on line 249

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /var/www/since2006.com/public_html/include/util/Ubb.php on line 249
使用ContentObserver监视未接电话,未读短信 - since.2006 -  

since.2006  

ContentObserver有点类似于BroadcastReceiver,当某类事件发生时进行调用.ContentObserver一般和系统或第三方程序提供的Provider一起使用.

当为某个URI注册了ContentObserver后,对其进行操作后都会调用注册的回调函数,以监视未接电话为例(未读短信和未接电话类似,只需将URI改为短信的URI就可以了):

// 注册ContentObserver
getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, false, new MissedCallContentObserver(this, 

new Handler()));

// 当有call log时,就会对这个类进行回调
public class MissedCallContentObserver extends ContentObserver {
 
 private Context ctx;
 
 private static final String TAG = "MissedCallContentObserver";
 
 public MissedCallContentObserver(Context context, Handler handler) {
  super(handler);
  ctx = context;
 }

 @Override
 public void onChange(boolean selfChange) {
  
  Cursor csr = ctx.getContentResolver().query(Calls.CONTENT_URI, new String[] {Calls.NUMBER, 

Calls.TYPE, Calls.NEW}, null, null, Calls.DEFAULT_SORT_ORDER);
  
  if (csr != null) {
   if (csr.moveToFirst()) {
    int type = csr.getInt(csr.getColumnIndex(Calls.TYPE));
    switch (type) {
    case Calls.MISSED_TYPE:
     Log.v(TAG, "missed type");
     if (csr.getInt(csr.getColumnIndex(Calls.NEW)) == 1) {
      Log.v(TAG, "you have a missed call");
     }
     break;
    case Calls.INCOMING_TYPE:
     Log.v(TAG, "incoming type");
     break;
    case Calls.OUTGOING_TYPE:
     Log.v(TAG, "outgoing type");
     break;
    }
   }
   // release resource
   csr.close();
  }
 }
 
 @Override
 public boolean deliverSelfNotifications() {
  return super.deliverSelfNotifications();
 }
}

Posted by hee at 18:03 PM | Permalink | 评论(2) | Android

5475 天 12 小时前
沙发 benny 说:

能否将 使用ContentObserver监视未读短信 的完整代码发给我看看,我在网上找到很多相似的例子,还是不能搞清楚,谢谢!!

5341 天 15 小时前
板凳 lidhsr 说:

能不能和我说下,onChange()的回调机制。
我想不清楚为什么我在做监听短信发送的时候会回调2次。
发我邮箱:lidhsr@163.com 谢谢啦

请输入名称
请输入邮件地址

 

    请输入邮件地址