---
title: "如何截取指定号码的短信，并且不让系统截取到通知用户"
author: "mars5337"
date: "2010-11-21T22:19:57Z"
tags: ["Android", "Blog"]
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2010-11-21T22:19:57Z"
id: 819148
url: "https://mars5337.iteye.com/blog/819148"
---

之前在做黑名单屏蔽短信的时候，遇到一个比较棘手的问题，就是我截取我指定号码的短信之后，但是我如何不让系统通过通知栏通知用户，在网上也寻找了很久，也有点眉目了，以下是别人使用 ContentObserver 来监听短信数据库变化来实现的。

 

```
public class ScreenTest extends Activity {   
  
　　class SmsContent extends ContentObserver{   
  
　　private Cursor cursor = null;   
  
　　public SmsContent(Handler handler) {   
  
　　super(handler);   
  
　　}   
  
　　/**  
 
　　* @Description 当短信表发送改变时，调用该方法  
 
　　* 需要两种权限  
 
　　* android.permission.READ_SMS 读取短信  
 
　　* android.permission.WRITE_SMS 写短信  
 
　　* @Author Snake  
 
　　* @Date 2010-1-12  
 
　　*/  
  
　　@Override  
  
　　public void onChange(boolean selfChange) {   
  
　　// TODO Auto-generated method stub   
  
　　super.onChange(selfChange);   
  
　　//读取收件箱中指定号码的短信   
  
　　cursor = managedQuery(Uri.parse("content://sms/inbox"), new String[]{"_id", "address", "read"}, " address=? and read=?", new String[]{"12345678901", "0"}, "date desc");   
  
　　if (cursor != null){   
  
　　ContentValues values = new ContentValues();   
  
　　values.put("read", "1"); //修改短信为已读模式   
  
　　cursor.moveToFirst();   
  
　　while (cursor.isLast()){   
  
　　//更新当前未读短信状态为已读   
  
　　getContentResolver().update(Uri.parse("content://sms/inbox"), values, " _id=?", new String[]{""+cursor.getInt(0)});   
  
　　cursor.moveToNext();   
  
　　}   
  
　　}   
  
　　}   
  
　　}   
  
　　/** Called when the activity is first created. */  
  
　　@Override  
  
　　public void onCreate(Bundle savedInstanceState) {   
  
　　super.onCreate(savedInstanceState);   
  
　　setContentView(R.layout.main);   
  
　　SmsContent content = new SmsContent(new Handler());   
  
　　//注册短信变化监听   
  
　　this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"),    
  
  
true, content);   
  
　　}   
  
　　}   
  
　　public class ScreenTest extends Activity {   
  
　　class SmsContent extends ContentObserver{   
  
　　private Cursor cursor = null;   
  
　　public SmsContent(Handler handler) {   
  
　　super(handler);   
  
　　}   
  
　　/**  
 
　　* @Description 当短信表发送改变时，调用该方法  
 
　　* 需要两种权限  
 
　　* android.permission.READ_SMS读取短信  
 
　　* android.permission.WRITE_SMS写短信  
 
　　* @Author Snake  
 
　　* @Date 2010-1-12  
 
　　*/  
  
　　@Override  
  
　　public void onChange(boolean selfChange) {   
  
　　// TODO Auto-generated method stub   
  
　　super.onChange(selfChange);   
  
　　//读取收件箱中指定号码的短信   
  
　　cursor = managedQuery(Uri.parse("content://sms/inbox"), new String[]{"_id", "address", "read"}, " address=? and read=?", new String[]{"12345678901", "0"}, "date desc");   
  
　　if (cursor != null){   
  
　　ContentValues values = new ContentValues();   
  
　　values.put("read", "1"); //修改短信为已读模式   
  
　　cursor.moveToFirst();   
  
　　while (cursor.isLast()){   
  
　　//更新当前未读短信状态为已读   
  
　　getContentResolver().update(Uri.parse("content://sms/inbox"), values, " _id=?", new String[]{""+cursor.getInt(0)});   
  
　　cursor.moveToNext();   
  
　　}   
  
　　}   
  
　　}   
  
　　}   
  
　　/** Called when the activity is first created. */  
  
　　@Override  
  
　　public void onCreate(Bundle savedInstanceState) {   
  
　　super.onCreate(savedInstanceState);   
  
　　setContentView(R.layout.main);   
  
　　SmsContent content = new SmsContent(new Handler());   
  
　　//注册短信变化监听   
  
　　this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);   
  
　　}   
  
　　}  
```

 [http://bingtian.iteye.com/blog/641566](http://bingtian.iteye.com/blog/641566)