0

I need a way to get the phone number of a caller and the date/time of the call after a call ends then start an activity and pass the data.

I know this can be done using Broadcast Receiver & Intent but how do I go about it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Inyavic
  • 11
  • 3
  • 2
    Possible duplicate of [How to get the caller's number?](https://stackoverflow.com/questions/30521487/how-to-get-the-callers-number) – Amir Oveisi Jun 23 '18 at 13:43

1 Answers1

0

You can do this using Accessibility Service

public class CallDetection extends AccessibilityService {
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
            Log.i("myaccess", "in window changed");


            AccessibilityNodeInfo info = event.getSource();
            if (info != null && info.getText() != null) {
                String duration = info.getText().toString();
                String zeroSeconds = String.format("%02d:%02d", new Object[]{Integer.valueOf(0), Integer.valueOf(0)});
                String firstSecond = String.format("%02d:%02d", new Object[]{Integer.valueOf(0), Integer.valueOf(1)});
                Log.d("myaccess", "after calculation - " + zeroSeconds + " --- " + firstSecond + " --- " + duration);
                if (zeroSeconds.equals(duration) || firstSecond.equals(duration)) {
                    Toast.makeText(getApplicationContext(), "Call answered", Toast.LENGTH_SHORT).show();
                    // Your Code goes here

                }
                info.recycle();
            }

        }
    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Toast.makeText(this, "Service connected", Toast.LENGTH_SHORT).show();
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        info.notificationTimeout = 0;
        info.packageNames = null;
        setServiceInfo(info);
    }

    @Override
    public void onInterrupt() {

    }
}
Raj
  • 2,997
  • 2
  • 12
  • 30