Content observer implements SMS listening and observer SMS listening
Content observer implements SMS monitoring
The content observer monitors whether the text message changes. If the text message changes, the content provider reads the text message.
Principle:
When a text message changes (for example, a text message), the text message application notifies the content observer that my text message has changed, and transmits the uri of the text message with the changed location, the content provider can easily read the text message content, but it must have sufficient permissions.
Package com. example. duanxinjianting; import android. app. activity; import android. database. contentObserver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. view. menu; import android. view. menuItem; public class MainActivity extends Activity {private MyObserver observer; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); observer = new MyObserver (new Handler (); // register a content observer (observe specified data) Uri uri = Uri. parse ("content: // sms"); getContentResolver (). registerContentObserver (uri, true, observer);}/*** content observer **/private class MyObserver extends ContentObserver {public MyObserver (Handler handler) {super (handler );} /*** when the observed data changes, this method is called back **/@ Override public void onChange (boolean selfChange, Uri uri Uri) {Log. d ("bh", uri. toString (); Cursor cursor = getContentResolver (). query (uri, null, null); while (cursor. moveToNext () {int index = cursor. getColumnIndex ("body"); String body = cursor. getString (index); Log. d ("bh", body) ;}}@ Override protected void onDestroy () {super. onDestroy (); // uninstall the content observer getContentResolver (). unregisterContentObserver (observer );}}