如何检测从您的Android设备完成的呼叫被应答或拒绝?

时间:2014-04-26 05:54:19

标签: android phone-call

我正在申请拨打多个号码。

在那个应用程序中

  1. 我打电话给1个人时,如果用户接听了电话 应该停止循环。

  2. 但是如果通话被拒绝,那么通话应该是下一个号码 循环应该是适当的。

  3. 我的问题是我无法检测到通话是否被拒绝或已被接听。当我在网上搜索时,有些人说无法检测到电话被接听或拒绝。

    是否真的无法在android中检测到调用如果有可能那么我该怎么做?

2 个答案:

答案 0 :(得分:2)

我认为你可以查看PhoneStateListener课程中最后一次通话的拨出电话时间' onCallStateChanged方法。如果状态为空闲TelephonyManager.CALL_STATE_IDLE,则获取数据。

这样的事情:

Cursor mCallCursor = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,null,null,null,null);
int duration = mCallCursor.getColumnIndex( CallLog.Calls.DURATION);
while(mCallCursor.moveToFirst())
{   
    Toast.makeText(context, mCallCursor.getString(duration), Toast.LENGTH_LONG).show();
}

您可以找到有关here的更多信息。我还没有测试过上面的代码。但这样的事情应该有效。

您可以检查时间是否为00:00,然后调用下一个循环次数。否则你可以停止打电话。

希望这会对你有所帮助。

答案 1 :(得分:0)

下面的

是通过辅助功能事件检测拨出电话的代码 -

在项目中添加一个扩展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 文件夹并添加名为的xml文件serviceconfig.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"
/>

您可以在Here

中找到有关 serviceconfig 的更多信息

现在在您的 Manifest 文件中添加您的服务 -

<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没有提供任何解决方案来检测呼叫是否得到应答,这是我做过的最佳选择,希望它对你有用。