[Android] using the broadcast receiver braodcast receiver to listen for calls from a mobile phone to cancel power-up

Source: Internet
Author: User

As the name suggests, braodcast receiver is a broadcast receiver. It is similar to the time processing mechanism, but the event processing mechanism is program component-level (for example, the Click Event of a button ), the broadcast event processing mechanism is system-level. We can use intent to start a component, or use the sendbroadcast () method to initiate a system-level event broadcast to transmit messages. We can also implement broadcast in our own applications.
Receiver to listen to and respond to the intent of the broadcast.

Event broadcasts are sent by creating an intent object and calling the sendbroadcast () method. Event acceptance is implemented by defining a class that inherits broadcastreceiver, inheriting the class and overwriting its onreceive () method to respond to the event.

public class PhoneReceiver extends BroadcastReceiver { @Overridepublic void onReceive(Context context, Intent intent){// TODO Auto-generated method stub}}

Next, we use a broadcast receiver to enable the call when the call status changes (call, answer, stop, and call). However, we cannot determine whether the recipient receives or transfers the call to the voice mailbox when the call is made. When the phone status changes, Android sends the action as Android. intent. action. phone_state broadcast. When you call the phone, the action is sent as Android. intent. action. new_outgoing_call broadcast, but I have read the development documentation and have not found any broadcast for incoming calls. We can simply write a broadcastreceiver to receive the two broadcasts.

Public class phonereceiver extends broadcastreceiver {@ override public void onreceive (context, intent) {// todo auto-generated method stub system. out. println ("action" + intent. getaction (); If (intent. getaction (). equals (intent. action_new_outgoing_call) {// if it is a power-off (outbound) system. out. println ("");} else {// check the android document. It seems that there is no action specifically used to receive incoming calls. out. println ("Incoming call"); telephonymanager TM = (telephonymanager) context. getsystemservice (service. telephony_service); TM. listen (listener, phonestatelistener. listen_call_state); // set a listener} phonestatelistener listener = new phonestatelistener () {@ override public void oncallstatechanged (INT state, string incomingnumber) {// todo auto-generated method stub // state the incomingnumber in the current state. It seems that there is no power-off API super. oncallstatechanged (State, incomingnumber); Switch (state) {Case telephonymanager. call_state_idle: system. out. println ("hanging up"); break; Case telephonymanager. call_state_offhook: system. out. println ("Answer"); break; Case telephonymanager. call_state_ringing: system. out. println ("Bell: Incoming call number" + incomingnumber); // outputs the incoming call number break ;}}};}

Because we are listening for system events, we need androidmanifest. XML to register the broadcast receiver:

        <receiver android:name=".PhoneReceiver">                <intent-filter>                        <action android:name="android.intent.action.PHONE_STATE"/>                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />                </intent-filter>        </receiver>


Do not forget to add permissions

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


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.