"The principle of Android content observer" introduces some basic principles of the content observer, and does a simple combat, this article then further to do a small project combat
PackageCom.wuyudong.smslistener;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;Importandroid.app.Activity;ImportAndroid.database.ContentObserver;ImportAndroid.database.Cursor;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Uri URI= Uri.parse ("Content://sms"); Getcontentresolver (). Registercontentobserver (URI,true, NewMyobserver (NewHandler ())); } Private classMyobserverextendsContentobserver { Publicmyobserver (Handler Handler) {Super(handler); } @Override Public voidOnChange (BooleanSelfchange) {System.out.println ("The SMS database has changed. "); Uri URI= Uri.parse ("Content://sms"); Cursor Cursor=getcontentresolver (). Query (URI,NewString[] {"Body", "address", "date", "type"},NULL, NULL, "_id desc"); Cursor.movetofirst (); String Body= cursor.getstring (0); String Address= cursor.getstring (1); String Date= Cursor.getstring (2); String type= Cursor.getstring (3); Try{ file file = new File (Getfilesdir (), "XXX.ABC"); FileOutputStream Fos=NewFileOutputStream (file,true); Fos.write (("Content:" + body + "--address" + adress + "--time:" +Date+ "--type" + type + "\ n"). GetBytes ()); Fos.close (); } Catch(Exception e) {e.printstacktrace (); } Super. OnChange (Selfchange); } }}
Add Permission: Android.permission.READ_SMS
After running the code, as long as the text message changes, will prompt, and the content of the text will be automatically saved in the Xxx.abc file
Android uses content viewer to implement SMS eavesdropping