如何从Call.Callback和InCallService TelecomManager获取呼叫事件?

时间:2018-06-11 11:35:07

标签: android

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

我不想替换当前用于拨打电话的UI,只是为了处理呼叫进度中的事件。我已经使用了PhoneStateListener,但只有3种状态( CALL_STATE_IDLE CALL_STATE_OFFHOOK CALL_STATE_RINGING )。

我希望能够访问Call.Details 这样的DisconnectCause

我现在不知道如何从我的Activity中绑定InCallService。这里有一些代码可以理解我想要做什么。

MyService Class

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>

知道如何才能获得此信息的访问权限吗? 抱歉我的英文!

1 个答案:

答案 0 :(得分:2)

InCallService API是Dialer / Phone应用程序用来为正在进行的电话实现来电UI的工具。除非用户选择系统作为默认拨号器,否则目前还没有方法可以使系统绑定到InCallService实现。