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.