Android 观察系统中短信内容的变化(内容观察者)
//内容观察者(如果系统的短信发生了变化,比如刚获取一条短信,那么将触发onChange方法) ContentResolver contentResolver = getContentResolver(); Uri uri = Uri.parse("content://sms/"); contentResolver.registerContentObserver(uri, true, new ContentObserver(new Handler()) { @Override public void onChange(boolean selfChange) { Toast.makeText(MainActivity.this, "系统信息已经改变", Toast.LENGTH_LONG).show(); //以下获取刚才变化的短信(比如是刚接收到的一条短信) String[] projection = {"_id", "address", "body", "date", "type"}; Cursor cursor = getContentResolver().query(Uri.parse("content://sms/"), projection, null, null, null); cursor.moveToFirst(); String body = cursor.getString(cursor.getColumnIndex("body")); System.out.println("body = " + body); cursor.close();
} });
本文出自 无忧之路 - 博客园