点击并按住耳机按钮显示无动作

时间:2014-09-13 20:51:16

标签: android button broadcastreceiver headset

我有一个BroadcastReceiver,我的目标是检测长按耳机按钮。尝试了不同的版本,没有一个工作。所以现在我正试图检查收到的行动。如果我正常点击,我可以看到动作0(KeyDown)和1(KeyUp)。如果我点击并按住,则无论是按键还是按键,都不显示任何内容。怎么了?

@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
        KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        Log.e("action",""+event.getAction());
        ...
    }
}

如果需要,我可以发布更多代码,但我认为它不相关

使用Android 4.3在S4上运行。不确定是否重要,S-Voice被冻结,Google Now被禁用。而且我注意到如果我的屏幕关闭并且我长按,我会收到一个菜单“使用Google搜索/自动发票处理长按”

1 个答案:

答案 0 :(得分:1)

我认为你可以通过清单中的这段代码控制长按。这将打开一个对话框,让您选择长按时使用的应用程序。当手机解锁时,它对我有用,但当手机锁定时它不能正常工作。它弹出对话框但是当你解锁手机时它会消失。希望我能得到一些帮助。

关键是打开“活动”而不是实现广播接收器。

   <activity 
            android:name="packagename.activityname"
              android:launchMode="singleInstance"
               android:showOnLockScreen="true">
             <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>
            <intent-filter>
                <action android:name="com.sec.action.SVOICE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

我在Kitkat上测试了这个。不知道以前版本会发生什么。