Android 擷取指定號碼的簡訊,並且不讓系統獲取簡訊通知使用者
Java 程式碼
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"), newString[]{"_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);
}
}