How to detect incoming calls, in an Android device?

前端 未结 10 1707
一向
一向 2020-11-22 02:27

I\'m trying to make an app like, when a call comes to the phone I want to detect the number. Below is what I tried, but it\'s not detecting incoming calls.

I want t

10条回答
  •  春和景丽
    2020-11-22 02:47

    this may helps you and also add require permision

    public class PhoneListener extends PhoneStateListener
    {
        private Context context;
        public static String getincomno;
    
        public PhoneListener(Context c) {
            Log.i("CallRecorder", "PhoneListener constructor");
            context = c;
        }
    
        public void onCallStateChanged (int state, String incomingNumber)
        {
    
            if(!TextUtils.isEmpty(incomingNumber)){
            // here for Outgoing number make null to get incoming number
            CallBroadcastReceiver.numberToCall = null;
            getincomno = incomingNumber;
            }
    
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
    
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d("CallRecorder", "CALL_STATE_RINGING");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
    
                break;
            }
        }
    }
    

提交回复
热议问题