谷歌现在不会让我的应用检测耳机按钮长按

时间:2014-01-08 09:13:17

标签: android android-broadcast long-click

我希望我的应用在用户长按(按住)耳机按钮时执行一些特定操作。我对BroadcastReceiver使用了MEDIA_BUTTON,它适用于那些不会长时间运行谷歌应用程序的手机。但是现在自动运行谷歌的手机我的应用程序被忽略了。如何立即停用Google并检测耳机按钮长按。这是onReceive课程中的BroadcastReceiver方法。

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();
}

3 个答案:

答案 0 :(得分:3)

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

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

   <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上测试了这个。不知道以前版本会发生什么。

答案 1 :(得分:2)

我相信你的努力可能是徒劳due to this Android bug

我认为这会覆盖您尝试的任何内容,或者最多只能启动您的应用以及Google即时....

我只是设法在root设备上解决这个问题,而我等待Google修复叹息

答案 2 :(得分:1)

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

关于背景,有序广播可以被接收器中止,这将阻止任何其他应用接收广播,我猜这就是Google Play正在做的事情。解决此问题的唯一方法是将您的清单中的android:priority设置为适合Google Play的价值,并希望Google Play开发者不会选择Integer.MAX_VALUE