我想实现一个简单的应用程序,允许我过滤来电。我曾尝试使用 BroadcastReceiver 检测通话,然后使用 TelecomManager 结束通话,但 TelecomManager 的 endCall() 需要系统权限。
我现在正在尝试按照本指南来实现调用应用程序,其中涉及实现 ConnectionService 类并使用它来创建/处理“连接”。Connections 代表一个电话呼叫,并有一个我打算用来过滤呼叫的 setDisconnected() 函数。
问题是我的 ConnectionService 类似乎没有正确绑定到 TelecomManager 并且它的函数没有被调用,因为日志语句没有发生。我怎样才能让它正确绑定?
我的 ConnectionService 实现:
public class CallHandler extends ConnectionService {
@Override
public Connection onCreateOutgoingConnection (PhoneAccountHandle connectionManagerPhoneAccount,
final ConnectionRequest request) {
return null;
}
@Override
public void onCreateOutgoingConnectionFailed (PhoneAccountHandle connectionManagerPhoneAccount,
final ConnectionRequest request) {
}
@Override
public void onCreateIncomingConnectionFailed (PhoneAccountHandle connectionManagerPhoneAccount,
final ConnectionRequest request) {
}
@Override
public Connection onCreateIncomingConnection (PhoneAccountHandle connectionManagerPhoneAccount,
final ConnectionRequest request) {
Log.i("~~~~INFO", "onCreateIncomingConnection was called!!!!");
return null;
}
public class TestConnection extends Connection {
@Override
public void onAnswer() {
}
@Override
public void onReject() {
}
}
}
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.oldphonetest">
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".CallHandler"
android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.ConnectionService" />
</intent-filter>
</service>
</application>
</manifest>
main的onCreate中注册CallHandler的代码:
private void register() {
Log.i("Info:", "Register Phone account called");
TelecomManager manager = (TelecomManager) getSystemService(TELECOM_SERVICE);
PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
new ComponentName(getPackageName(),
CallHandler.class.getName()), "CallHandlerId");
PhoneAccount.Builder builder = PhoneAccount.builder(phoneAccountHandle, "CustomAccount");
builder.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED);
PhoneAccount phoneAccount = builder.build();
manager.registerPhoneAccount(phoneAccount);
}