模拟KEYCODE_HEADSETHOOK长按

问题描述

我正在尝试在我的配套应用中以编程方式控制来电(接受和拒绝)目标 Android 6.0 及更高版本。

工作方法但已弃用

telecomManager.acceptCall() 和 telecomManager.endCall() 这种方法在 Android 10 和虚拟 Android 11 之前都可以正常工作,但在开发者网站上它说它已被弃用。

This method was deprecated in API level 29.
Companion apps for wearable devices should use the InCallService API instead.

部分工作方法

通过模拟耳机按键按键事件发现可以控制通话。以下是我的实现

void sendHeadsetHookLollipop() {
    MediaSessionManager mediaSessionManager =  (MediaSessionManager) getApplicationContext().getSystemService(Context.MEDIA_SESSION_SERVICE);

 

    try {
        List<MediaController> mediaControllerList = mediaSessionManager.getActiveSessions 
                     (new ComponentName(getApplicationContext(),NotificationReceiverService.class));

 

        for (MediaController m : mediaControllerList) {
             if ("com.android.server.telecom".equals(m.getPackageName())) {
                 m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK));
                 log.info("HEADSETHOOK sent to telecom server");
                 break;
             }
        }
    } catch (SecurityException e) {
        log.error("Permission error. Access to notification not granted to the app.");      
    }  
}

在上面的一段代码中,我能够接听正在响铃的电话。要拒绝,我需要模拟长按相同的 KeyEvent。

1.如何实现一个keyEvent的长按?

2.对于上述需求,是否还有其他非弃用的实现方法?

3.在telecomManager类中,他们建议实现InCallService。如何在不将我的应用设为默认应用的情况下实现 InCallService?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...