1

我已在manifest.xml文件中授予权限

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

并定义一个接收器:

<receiver android:name=".OutgoingCallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

接收文件是:

public class OutgoingCallReceiver extends BroadcastReceiver {

public static String TAG = "AUTODIAL";
static long start_time, end_time, call_duration;
String number;

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "intent get data : "+intent.getData(), Toast.LENGTH_LONG).show();
    String action = intent.getAction();
    String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    Toast.makeText(context, "Phone State : "+state, Toast.LENGTH_LONG).show();

    if(state==null) {
        number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Toast.makeText(context, "Out going Phone number : "+number, Toast.LENGTH_LONG).show();
    }

    if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            start_time = System.currentTimeMillis();
        }
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            end_time = System.currentTimeMillis();
        }
        call_duration = end_time - start_time;
}
}

在这里,它提供了全部通话时间,而不仅仅是我从我的应用程序拨打的通话时间。

拨号后是否可以返回我的应用程序?

4

1 回答 1

2

下面是通过可访问性事件检测拨出电话的代码 -

AccessibilityService添加一个在您的项目中扩展的类-

public class CallDetection extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
     acquireLock(this);
    Log.d("myaccess","after lock");
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
        Log.d("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() {

}
}

但是要使功能event.getSource()正常工作,您必须通过 xml 指定一些服务配置,因此在您的项目中创建一个xml文件夹并添加一个名为 serviceconfig.xml的 xml 文件(您可以给任何您想要的名称。

serviceconfig 的内容如下 -

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/callDetection"
android:accessibilityEventTypes="typeWindowContentChanged"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
/>

您可以在此处找到有关serviceconfig的更多信息

现在将您的服务添加到您的清单文件中,如下所示 -

<service android:name=".CallDetection"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="@string/callDetection">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/serviceconfig" />
</service>

大功告成,只需运行应用程序并转到手机中的辅助功能设置,您将找到一个名为检测的选项(或您作为服务描述提供的任何名称),将其打开以授予您应用程序的辅助功能权限。

现在,当您接听电话时,您会看到祝酒词。

您可以在其中编写任何您想要的代码,也可以在活动中调用回调函数

最重要的是- 在接听电话之前不要拨打您的通话窗口(android 拨号器窗口),否则这将不起作用。

注意- 由于 android 没有提供任何解决方案来检测呼叫是否被应答,这是我所做的最好的选择,希望它对你有用。

于 2017-07-09T17:09:31.610 回答