Android development-listening for phone calls and android-listening for incoming calls

Source: Internet
Author: User

Android development-listening for phone calls and android-listening for incoming 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:

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 mobile phone calls * This instance monitors the current phone call status, * write the power call 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

<! -- Grant the application permission to read the call status --> <uses-permission android: name = "android. permission. READ_PHONE_STATE"/>

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 .............


Android: monitors incoming call events

// The following demo can listen to the call time, which can be written in the Service. when the Service is started, register the broadcast receiver. If you do not know it, check the broadcast receiver (BroadcastReceiver) you can also register the service and broadcast receiver import android in the configuration file. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. telephonyManager; import android. util. log; public class BroadcastReceiverMgr extends BroadcastReceiver {private final String TAG = MyBroadcastReceiver. TAG; @ Overridepublic void onReceive (Context context, Intent intent) {String action = intent. getAction (); Log. I (TAG, "[Broadcast]" + action); // if (action. equals (MyBroadcastReceiver. B _PHONE_STATE) {Log. I (TAG, "[Broadcast] PHONE_STATE"); doReceivePhone (context, intent) ;}/ *** process the phone Broadcast. * @ param context * @ param intent */public void doReceivePhone (Context context, Intent intent) {String phoneNumber = intent. getStringExtra (TelephonyManager. EXTRA_INCOMING_NUMBER); TelephonyManager telephony = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE); int state = telephony. getCallState (); switch (state) {case TelephonyManager. CALL_STATE_RINGING: Log. I (TAG, "[Broadcast] waiting for answering phone =" + phoneNumber); break; case TelephonyManager. CALL_STATE_IDLE: Log. I (TAG, "[Broadcast] hanging up by phone =" + phoneNumber); break; case TelephonyManager. CALL_STATE_OFFHOOK: Log. I (TAG, "[Broadcast] In-call =" + phoneNumber); break ;}}}

In the Android app, what is the status of a listener call?

In fact, when you use these software, the telephone software will pause playing, pause transmission, or return to the background, so as not to conflict with the electricity. It is unknown that there are no other insiders or tricks.

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.