Android call and power-off monitoring

Source: Internet
Author: User

Add the phone call status to the Android phone and use phonestatelistener to listen.

Telephonymanager = (telephonymanager) context. getsystemservice (telephony_service); // start to listen to phonestatelistener phonelistener = new myphonestatelistener (); telephonymanager. listen (phonelistener, phonestatelistener. listen_call_state); // cancel listening to telephonymanager. listen (phonelistener, phonestatelistener. listen_none );

The above myphonestatelistener is an internal class that inherits phonestatelistener.

Class encryptphonestatelistener extends phonestatelistener {encryptphonestatelistener () {telephonymanager = (telephonymanager) getsystemservice (context. telephony_service);} public void oncallstatechanged (INT state, string incomingnumber) {Switch (state) {Case telephonymanager. call_state_offhook: // mclog when calling or answering the call. I ("state = call_state_offhook"); break; Case telephonymanager. call_state_ringing: // mclog when the call comes in. I ("state = call_state_ringing"); break; Case telephonymanager. call_state_idle: // when the call is suspended, or mclog is not reflected. I ("state = call_state_idle"); break; default: Break ;}}}

Oncallstatechanged is a callback method, and incomingnumber is the incoming call number.

The above method is only suitable for listening for incoming calls, not for power-off. To listen for power-off, you need to add additional items in the above method.

Android. Intent. Action. new_outgoing_call defines a broadcast receiver. The accepted action is Android. Intent. Action. new_outgoing_call.

Public class mainreceiver extends broadcastreceiver {public void onreceive (context, intent) {string action = intent. getaction (); If (intent. action_new_outgoing_call.equals (Action) {// power-off string number = intent. getstringextra (intent. extra_phone_number );}}}

In intent, the phone number is passed, and the phone status above is combined to determine when the power is disconnected and hung up.

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.