Android listening phone status

Source: Internet
Author: User

[Java]
TelephonyManager telephonyManager = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE );
TelephonyManager. listen (new PhoneListener (), PhoneStateListener. LISTEN_CALL_STATE );

[Java]
Private class PhoneListener extends PhoneStateListener {
@ Override www.2cto.com
Public void onCallStateChanged (int state, String incomingNumber ){
Super. onCallStateChanged (state, incomingNumber );

Switch (state ){
Case TelephonyManager. CALL_STATE_RINGING: // call status

Break;
Case TelephonyManager. CALL_STATE_OFFHOOK: // Answer status

Break;
Case TelephonyManager. CALL_STATE_IDLE: // return to idle state after hanging up
 
Break;

Default:
Break;
}
}

}

The above code is a listener for phone status changes. Here we need to understand the listener mechanism.
Android has two types of event processing mechanisms: listener and callback.

A listener-based event processing

Three types of objects are involved: EventSource, Event, and EventListener)

The process of event processing by the listener is as follows (delegate: Delegation ):

 

 

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.