我已经在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;
}
}
在这里,它给出了所有通话时长,而不仅仅是我从我的应用程序调用的通话时长。
拨号后是否可以返回到我的应用程序?
最佳答案
下面是通过可访问性事件检测呼出电话的代码-
在您的项目中添加一个扩展 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"
/>
您可以在 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>
大功告成,只需运行该应用并转到手机中的辅助功能设置,您会找到一个名为检测(或您想要的任何名称)的选项已作为您的服务说明给出),将其打开以授予您应用程序的可访问性权限。
现在您会在接听电话时看到 toast 。
您可以在其中编写任何您想要的代码,也可以在您的 Activity 中调用回调函数
最重要 - 在电话被接听之前不要调用您的调用窗口(android 拨号器窗口),否则这将不起作用。
注意 - 由于 android 不提供任何解决方案来检测调用是否已接听,这是我做出的最佳选择,希望它对你有用。
关于android - 如何以编程方式计算android中的拨出电话(我从我的应用程序调用)持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690486/