Android development-listening for mobile phone calls

Source: Internet
Author: User

Android development-listening for mobile phone calls

TelephonyManager is a service class that manages the call status and network information of a mobile phone. It provides a large number of getXxx () methods to obtain information about the telephone network. For details about TelephonyManager, refer to Android development to obtain SIM card information of mobile phones.

Program running result:

Log_phone file:


Application instance: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpghypgo8cd48l3a + CjxwcmUgY2xhc3M9 "brush: java;"> 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:
* Monitoring mobile phone calls * This instance monitors the current phone call status, and writes future calls and answer records to the log_phone file * @ author jph * Date: 2014.07.22 **/public class MonitorPhone extends Activity {TelephonyManager tManager; private String incomingNumber; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the system's TelephonyManager object tManager = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE); // create a call status 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: // call writeFile (state, number); break; case TelephonyManager. CALL_STATE_RINGING: // incoming call incomingNumber = number; writeFile (state, number); break; default: break;} super. onCallStateChanged (state, incomingNumber) ;}}; // Add listener tManager for tManager. listen (pListener, PhoneStateListener. LISTEN_CALL_STATE);} // write the incoming call information to the file protected void writeFile (int state, String number) {// TODO Auto-generated method stubStringBuffer sb = new 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: // calls sb. append ("received phone number:" + incomingNumber + ""); break; case TelephonyManager. CALL_STATE_RINGING: // call sb. append (number + "Incoming call"); break;} sb. append ("\ n --------------------- \ n"); FileOutputStream fos = null; try {// open the output stream fos = openFileOutput ("log_phone", MODE_APPEND) in append mode );} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}// encapsulate the output stream into the PrintStream object PrintStream ps = new PrintStream (fos); // The input file content ps. println (sb. toString (); // close the output stream ps. close ();}}
Finally, do not forget to add the corresponding permissions to the application:

AndroidManifest. xml

 
  

Instance analysis:

This application is implemented through Activity. If you put the code in the instance to the Service that is executed in the background and set the Service component to start with the system, then this listener can be "unknown, you don't know. Hey .............

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.