通话结束后获取来电者的电话号码,然后开始一项活动

时间:2018-06-23 09:36:27

标签: android android-intent broadcastreceiver phone-state-listener

我需要一种方法来获取呼叫者的电话号码以及呼叫结束后的呼叫日期/时间,然后启动活动并传递数据。

我知道可以使用“广播接收器和意图”来完成此操作,但是我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

您可以使用辅助功能

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() {

    }
}