且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中以编程方式计算拨出电话(我从我的应用拨打的电话)的持续时间

更新时间:2022-03-04 23:14:02

以下是通过辅助功能事件检测去电的代码-

below is a code of detecting outgoing call by accessibility events -

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

Add a class which extends AccessibilityService in your projects -

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 (您可以输入所需的任何名称.

But to get the function event.getSource() working you have to specify some of your service configuration through xml, so create a xml folder in your project and add a xml file called serviceconfig.xml (you can give any name you want.

serviceconfig的内容如下-

The content of serviceconfig is below -

<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 的更多信息. >这里

You can find more about serviceconfig in Here

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

Now add your service in you Manifest file like this -

<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>

完成后,只需运行该应用程序并转到手机中的辅助功能设置,您将找到一个名为检测(或您所使用的名称)的选项.作为您的服务说明),将其打开以为您的应用提供访问权限.

And youre done, just run the app and go to Accessibility settings in your phone, you will find an option named as detection (or whatever name you have given as your service description), switch that on to give accesibility permissions for you app.

现在,接听电话后您将看到烤面包.

Now you will see a toast when call is answered.

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

you can Code any code you want in there, also you can call a callback function in your activity

最重要-在呼叫被应答之前,请勿呼叫您的呼叫窗口(android拨号器窗口),否则将无法正常工作.

Most important - Dont call your call window(android dialer window) untill the call is answered, otherwise this will not work.

注意-由于android没有提供任何解决方案来检测呼叫是否已应答,因此这是我做出的***选择,希望它对您有用.

Note - As android doesn't provide any solution to detect if the call is answered or not, this is the best alternative i have made, hope it works for you.