public class Monitorphone extends Activity {telephonymanager tmanager; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);// Get Telephonemanager Object Tmanager = (Telephonymanager) getsystemservice (context.telephony_service);// Create a call status listener phonestatelistener listener = new Phonestatelistener () {@Overridepublic void oncallstatechanged (int state, String incomingnumber) {switch (state) {//no status case telephonymanager.call_state_idle:break;case telephonymanager.call_state_offhook:break;//the case TelephonyManager.CALL_STATE_RINGING:OutputStream OS = null when the call bell rings; Try{os = Openfileoutput ("Phonelist", Mode_append);} catch (FileNotFoundException e) {e.printstacktrace ();} PrintStream PS = new PrintStream (OS);p s.println (New Date () + "Incoming call:" + incomingnumber);p s.close (); break;default:break;}; Super.oncallstatechanged (state, incomingnumber);}};/ /Monitor phone call status change Tmanager.listen (Listener, phonestatelistener.listen_call_state);}}
Mainfest file
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Crazyit.monitorphone "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/><uses-permission android:name=" Android.permiss Ion. Read_phone_state "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launcher " Android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:n Ame= "Crazyit.monitorphone.MonitorPhone" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Androi D.intent.category.launcher "/> </intent-filter> </activity> </application></m Anifest>
Telephonymanager is a service class that manages cell phone communication status and telephone network information, and this class provides a listen (Phonestatelistener listener,int events) method to monitor call status
Run the above program, you can see a phonelist file in the Ddms's File Explorer panel data/data/crazyit.monitorphone/files directory, the file will be imported to the computer can see the phone call information
Because I am testing with a real machine, I need root permission to export the file under DDMS, otherwise I can't open the folder
To get permission to open a folder in DDMS:
>ADB Shell
$ su
# chmod 755/data
# chmod 755/data/data
# chmod 755/data/data/crazyit.monitorphone/
# chmod 755/data/data/crazyit.monitorphone/files
service-Monitor Phone calls