简体   繁体   中英

Handling incoming calls in Android

I want to handle incoming call in Android.
Actually I want to set a time duration in which if my cell phone receive any call then automatically a message send to each of them.

Any ideas?

Just extend your class to PhoneStateListener and override onCallStateChanged method. Sample code:

class myCallListener extends PhoneStateListener{

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // TODO Auto-generated method stub
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                    // your logic here
                break;

            default:
                break;
            }

            super.onCallStateChanged(state, incomingNumber);

        }
    }

You need to declare PhoneStateListener in your Activity or Service :

PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            ....
        } else if(state == TelephonyManager.CALL_STATE_IDLE) {
            ....
        } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
            ....
        }
        super.onCallStateChanged(state, incomingNumber);
    }
};

And add following permission to AndroidManifest.xml :

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM