Android obtains the phone status and listens to the phone call status, and android listens

Source: Internet
Author: User

Android obtains the phone status and listens to the phone call status, and android listens

Get Mobile Phone status:

Import android. content. context; import android. telephony. telephonyManager; // obtain the corresponding system service TelephonyManager tm = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE);/*** return call status ** when CALL_STATE_IDLE is not in any status * When CALL_STATE_OFFHOOK receives the call * When CALL_STATE_RINGING comes in */tm. getCallState (); if (tm. getCallState () = TelephonyManager. CALL_STATE_IDLE) {Log. d ("test", "call state idle... ");} else if (tm. getCallState () = TelephonyManager. CALL_STATE_OFFHOOK) {Log. d ("test", "call state offhook... ");} else if (tm. getCallState () = TelephonyManager. CALL_STATE_RINGING) {Log. d ("test", "call state ringing... ");}


Listen to the phone call status:

// Obtain the corresponding system service TelephonyManager tm = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE); // use the listen (PhoneStateListener listener, int events) of the TelephonyManager object) // implement the PhoneStateListener listener and implement the corresponding method public class MyPhoneCallListener extends PhoneStateListener {@ Override public void onCallStateChanged (int state, String incomingNumber) {switch (state) {case TelephonyManager. CALL_STATE_OFFHOOK: // call status Toast. makeText (Main. this, "talking... ", Toast. LENGTH_SHORT ). show (); break; case TelephonyManager. CALL_STATE_RINGING: // call status Toast. makeText (Main. this, incomingNumber, Toast. LENGTH_SHORT ). show (); break;} super. onCallStateChanged (state, incomingNumber );}


The first parameter needs to implement the PhoneStateListener listener and implement the corresponding method. The second parameter is the static constant of PhoneStateListener. In this case, because it is a listener, you need to input LISTEN_CALL_STATE, at the same time, you also need to register the corresponding permissions in AndroidManifest

<Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>


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.