1

当用户长按(按住)耳机按钮时,我希望我的应用程序执行一些特定操作。我使用了BroadcastReceiverfor MEDIA_BUTTON,它在没有运行 Google now 应用程序的手机上运行良好,可以进行长按的声音操作。但是在现在自动运行 Google 的手机中,我的应用程序只是被忽略了。我现在如何禁用谷歌并检测耳机按钮长按。这是onReceiveBroadcastReceiver班上的方法。

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 action = event.getAction();

    switch (event.getKeyCode()) {
    case KeyEvent.KEYCODE_HEADSETHOOK:
             /**
             * for long click
             */
            if (action == KeyEvent.ACTION_DOWN) {

                if (!isDown) {
                    isDown = true;
                    handler.postDelayed(l, LONG_CLICK_DELAY);
                }

            }

            if (action == KeyEvent.ACTION_UP) {

                                     /**
                 * for long click
                 */
                if (isDown)
                    isDown = false;
             }
            break;
    }
    abortBroadcast();
}
4

3 回答 3

3

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

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

   <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 上对此进行了测试。不知道以前的版本会发生什么。

于 2014-10-06T07:12:50.133 回答
2

我相信你的努力可能会因为这个 Android 错误而白费。

我认为这将覆盖您尝试的任何内容,或者充其量启动您的应用程序以及 Google Now....

我只能在有根设备上解决这个问题,同时等待谷歌修复

于 2014-01-16T11:09:47.853 回答
1

您可以覆盖 onKeyDown 并查找 KeyEvent.KEYCODE_HEADSETHOOK 键来处理您的应用程序处于前台时的情况。

至于背景,接收器可以中止有序广播,这将阻止任何其他应用程序接收广播,我猜这就是 Google Play 正在做的事情。解决这个问题的唯一方法是android:priority在您的清单中设置一个合适的高值,以出现在 Google Play 之前,并希望 Google Play 开发人员没有选择Integer.MAX_VALUE

于 2014-01-10T19:00:30.887 回答