首页 > 解决方案 > 如何从 Call.Callback 和 InCallService TelecomManager 获取呼叫事件?

问题描述

我正在使用 Android 应用程序,我想处理一些电话呼叫事件。

我不喜欢替换当前的 UI 来拨打电话,只是为了处理通话进度的事件。我已经使用 PhoneStateListener 但只有 3 个状态(CALL_STATE_IDLECALL_STATE_OFFHOOKCALL_STATE_RINGING)。

我想访问Call.Details 之类的DisconnectCause

我现在不知道如何从我的 Activity 绑定 InCallService。这是一些代码来理解我想要做什么。

我的服务类

public class MyService extends InCallService {


public static final String TAG = "MyService";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    Log.d(TAG, "On Start");

    return START_STICKY;
}




@Override
public void onCallAdded(Call call) {
    super.onCallAdded(call);


    call.registerCallback(new CallbackTelecomHelper(this));

    Log.d(TAG, "onCallAdded");
    Log.d(TAG, "onCallAdded details" + call.getDetails());
}


@Override
public void onCallRemoved(Call call) {
    super.onCallRemoved(call);
    Log.d(TAG, "onCallRemoved");
    Log.d(TAG, "onCallRemoved details" + call.getDetails());
}

@Override
public void onConnectionEvent(Call call, String event, Bundle extras) {
    super.onConnectionEvent(call, event, extras);

    Log.d(TAG, "getDisconnect code: " + call.getDetails().getDisconnectCause().getCode());
    Log.d(TAG, "getDisconnect reason: " + call.getDetails().getDisconnectCause().getReason());
    Log.d(TAG, "getDisconnect description: " + call.getDetails().getDisconnectCause().getDescription());
    Log.d(TAG, "event : " + event);

}

CallbackTelecomHelper - Call.Callback的帮助器类

public class CallbackTelecomHelper extends Call.Callback {

String TAG = "CallbackTelecomHelper_TAG";

private Context context;

public CallbackTelecomHelper(Context context){
    this.context = context;
}

@Override
public void onStateChanged(Call call, int state) {
    super.onStateChanged(call, state);

    Log.i(TAG, "onStateChanged");
}

@Override
public void onDetailsChanged(Call call, Call.Details details) {
    super.onDetailsChanged(call, details);


    Log.i(TAG, "onDetailsChanged");
}

@Override
public void onCallDestroyed(Call call) {
    super.onCallDestroyed(call);

    Log.i(TAG, "onCallDestroyed");
}

@Override
public void onConnectionEvent(Call call, String event, Bundle extras) {
    super.onConnectionEvent(call, event, extras);


    Log.i(TAG, "onConnectionEvent");
}

@Override
public void onRttRequest(Call call, int id) {
    super.onRttRequest(call, id);


    Log.i(TAG, "onRttRequest");
}

从我的活动我开始服务:

TelecomManager telecomManager = (TelecomManager) getSystemService(TELECOM_SERVICE);
telecomManager.getDefaultDialerPackage();
Intent intentConnection =  new Intent(this, MyService.class);
startService(intentConnection);

清单设置:

 <activity
        android:name=".ui.ScenarioMapActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.CALL" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

    </activity>

 <service android:name=".services.Connection.MyService"
        android:permission="android.permission.BIND_INCALL_SERVICE">
        <meta-data android:name="android.telecom.IN_CALL_SERVICE_UI" android:value="true" />
        <intent-filter>
            <action android:name="android.telecom.InCallService"/>
        </intent-filter>
    </service>

知道如何访问这些信息吗?对不起我的英语不好!

标签: android

解决方案


InCallService API 是 Dialer/Phone 应用程序用来为正在进行的电话实现 incall UI 的。目前没有办法让系统绑定到 InCallService 实现,除非用户选择它作为默认拨号程序。


推荐阅读