Telephonymanager is a service class that manages mobile phone call status and telephone network information, which provides a large number of getxxx () methods to obtain information about the telephone network. the details of Telephonymanager can be referenced: Android development to get SIM card information for mobile phone
Program Run Result:
Log_phone file:
Application Examples:
Package Com.jph.monitorphone;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.printstream;import Java.text.simpledateformat;import Java.util.date;import Android.os.Bundle;import Android.app.activity;import Android.content.context;import Android.telephony.phonestatelistener;import android.telephony.telephonymanager;/** * describe:</br> * Monitor phone calls * This example enables monitoring of the current phone's call status * and writes the call and answer record to the Log_phone file * @author JPH * date:2014.07.22 * */public class Monitorphone extends Activity {telephonymanager tmanager;private String Inc Omingnumber; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);//Gets the Telephonymanager object of the system tmanager= (Telephonymanager) Getsystemservice ( Context.telephony_service);//Create a call state listener Phonestatelistener plistener=new Phonestatelistener () {@Overridepublic void oncallstatechanged (int state, String number) {//TODO auto-generated Method Stubswitch (state) {case Telephonymanager . Call_state_idle://No status break;case telephonymanager.call_state_offhook://answer call WriteFile (State,number); break;case telephonymanager.call_state_ringing://call Incomingnumber=number;writefile (state,number); break;default:break;} Super.oncallstatechanged (state, incomingnumber);}};/ /Add Listener Tmanager.listen (Plistener, phonestatelistener.listen_call_state) for Tmanager;} Will answer the call, with the caller information written to the file protected void WriteFile (int state, String number) {//TODO auto-generated method Stubstringbuffer sb=n EW StringBuffer (); SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd hh.mm.ss"); Sb.append ("Time:" +sdf.format (New Date ()) + "\ n"); Switch (state) {case telephonymanager.call_state_offhook://answer call Sb.append ("answer phone number:" +incomingnumber+ "); Case telephonymanager.call_state_ringing://Call Sb.append (number+ "call"); Sb.append ("\ n-----------------------\ n"); FileOutputStream Fos=null;try {//Opens the output stream fos=openfileoutput ("Log_phone", Mode_append) in an additional way, and the catch ( FileNotFoundException e) {//TODO auto-generated catch Blocke.printstackTrace ();} Encapsulates the output stream into a PrintStream object PrintStream ps=new printstream (FOS);//Input file Contents Ps.println (sb.tostring ());//Close output stream ps.close ();}}
Finally, don't forget to add the appropriate permissions for your app:
Androidmanifest.xml
<!--grant the app permission to read the call status--><uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>
Example Analysis:
This application is implemented through activity, if you put the code in the instance to run in the background service, and set the service component to follow the system to start, that this kind of monitoring can be done "God did not know, unnoticed." Hey.............
Android Development Monitor Phone calls