0

I want to detect an incoming phone call and I want to know the phonenumber. I also want to know when the call is finished.

I found this code here: https://stackoverflow.com/a/11182720/10858753

It works fine in the beginning but after some calls I don't get any response from the class anymore.

Can anybody help me with it? Thanks in advance!

PhoneCallListener

public class PhoneCallListener extends PhoneStateListener {

    private static final String LOG_TAG = PhoneCallListener.class.getSimpleName();
    private boolean isPhoneCalling = false;
    private Context context;

    PhoneCallListener(Context context) {
        this.context = context;
        Log.d(LOG_TAG, "PhoneCallListener initiated");
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        Log.d(LOG_TAG, "onCallStateChange " + state);

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(LOG_TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended, need detect flag
            // from CALL_STATE_OFFHOOK
            Log.i(LOG_TAG, "IDLE number");

            if (isPhoneCalling) {

                Handler handler = new Handler();

                //Put in delay because call log is not updated immediately when state changed
                // The dialler takes a little bit of time to write to it 500ms seems to be enough
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // get start of cursor
                        Log.i("CallLogDetailsActivity", "Getting Log activity...");
                        String[] projection = new String[]{CallLog.Calls.NUMBER};


                        @SuppressLint("MissingPermission") Cursor cur = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, projection, null, null, CallLog.Calls.DATE + " desc");
                        if (cur != null) {
                            cur.moveToFirst();
                            String lastCallnumber = cur.getString(0);
                            Log.d(LOG_TAG, "Call ended: " + lastCallnumber);
                            cur.close();
                        }
                    }
                }, 500);

                isPhoneCalling = false;
            }

        }
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    private TextView tv;

    private Receiver phoneReceiver, smsReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = findViewById(R.id.tv);

        PhoneCallListener phoneListener = new PhoneCallListener(this);
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }
}
Robbe
  • 53
  • 7
  • check if your device has some battery-saver app or something like that built-in, such apps / device features may kill apps that are not currently running, preventing them from getting notified for broadcasts and such – marmor Jan 30 '20 at 14:01
  • I was using an emulator so I don't think that is the reason. – Robbe Jan 30 '20 at 15:53
  • did you at some point force-close your app? after doing that no listener or broadcast receiver will work – marmor Jan 30 '20 at 17:59
  • I think it was something like that becaus I tried again and everything worked fine. Thanks! – Robbe Feb 01 '20 at 19:48
  • yep, common issue of confusion unless you've been hot by this already and wasted a few hours trying to figure it what went wrong – marmor Feb 01 '20 at 21:40
  • I've one last question. Do I have to unregister the listener? – Robbe Feb 02 '20 at 06:22

0 Answers0