我已经在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;
}
}在这里,它给出了所有的通话时间,而不仅仅是我从我的应用程序中拨打的。
从我的应用程序拨号后,它能返回到我的应用程序吗?
发布于 2017-07-09 17:09:31
下面是通过可访问性事件检测传出呼叫的代码-
在项目中添加一个扩展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() {
}
}但是要使函数serviceconfig.xml正常工作,您必须通过xml指定一些服务配置,所以在项目中创建一个 xml 文件夹,并添加一个名为event.getSource()的文件(您可以给出任何您想要的名称)。
服务的内容如下-
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/callDetection"
android:accessibilityEventTypes="typeWindowContentChanged"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
/>现在将您的服务添加到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>您已经完成了,只需运行该应用程序,并在您的手机上转到辅助设置,您就会发现一个名为 permissions 的选项(或者您为您的服务描述提供的名称),打开它为您的应用程序提供可访问性权限。
现在,您将看到祝酒词时,呼叫被接听。
您可以在其中编码任意代码,也可以在活动中调用回调函数。
最重要的-不要调用您的呼叫窗口(安卓拨号窗口),直到呼叫被回答,否则这将无法工作。
Note -由于android没有提供任何解决方案来检测呼叫是否被应答,这是我做过的最好的选择,希望它对您有用。
https://stackoverflow.com/questions/44690486
复制相似问题