Android线控耳机长按事件

Android线控耳机长按事件

1,在这个周的工作中,这个问题让我纠结了好久,问度娘不知道,上android开发者官网似乎进入了词海,翻了下上了个GOOGLE,还是没有很好的答案。

2,后来得到一个相关的APK,研究过后发现原来跟线控耳机长按时,出来百度语音搜索应该一个原理(有装百度手机客户端的话)。

3,主要就两个ACTION就可以实现长按操作,而且类似于后台服务的效果(如果界面显示的话,直接LongPress就可以处理了):

  在Manifest中的一个activity下插入如下代码

复制代码
 <intent-filter android:priority="2147483647">
                <action android:name="android.speech.action.WEB_SEARCH" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter android:priority="2147483647">
                <action android:name="android.speech.action.VOICE_SEARCH_HANDS_FREE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
复制代码

4,当然,这个activity的dialog风格可以随意,比如弄成一个弹框界面大小的风格 或者 完全透明的风格(在onStart中执行完相应操作后并finish,就类似于一种看

              不见的服务效果,而且不会因主程序被后台清理掉而收不到响应),跟一同事聊天的时候,他说可以搞一个偷拍,这个。。。。。。

 

5,上面的代码紧针对Android4.1及以上的系统(因为线控耳机的长按操作被GOOGLE当作语音搜索保留),Android4.1以下的版本则只需响应一个叫MEDIA_BUTTON

  的服务即可,此时算下DOWN 和 UP的时间即可处理成长按操作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 12中的线控耳机双击事件可以通过注册BroadcastReceiver来实现。具体步骤如下: 1. 在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.RECORD_AUDIO"/> ``` 2. 在BroadcastReceiver中注册广播接收器: ```java public class EarphoneButtonReceiver extends BroadcastReceiver { private static final String TAG = EarphoneButtonReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { String intentAction = intent.getAction(); if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { return; } KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (event == null) { return; } int keycode = event.getKeyCode(); int action = event.getAction(); if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_HEADSETHOOK) { //线控耳机双击事件触发的代码 Log.d(TAG, "Earphone double click detected."); } } } ``` 3. 在Activity或Service中注册BroadcastReceiver: ```java private EarphoneButtonReceiver mEarphoneButtonReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mEarphoneButtonReceiver = new EarphoneButtonReceiver(); IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON); registerReceiver(mEarphoneButtonReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mEarphoneButtonReceiver); } ``` 这样就可以在双击线控耳机按钮时触发相应的事件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值