Handling incoming calls in Android

邮差的信 提交于 2019-12-04 06:36:21

问题


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?


回答1:


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);

        }
    }



回答2:


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.



来源:https://stackoverflow.com/questions/5716481/handling-incoming-calls-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!