检测耳机点击并在安卓上打开一个活动

huangapple go评论63阅读模式
英文:

Detecting headphone click and opening an activity on android

问题

我在这里遇到了一个问题,我正在尝试创建一个类似Google助手的东西,一旦按下耳机的播放或暂停按钮,广播接收器应该捕捉到并启动一个新的活动。

以下是我目前的代码:

清单文件

<receiver android:name=".services.MediaButtonReceiver">
    <intent-filter android:priority="2147483647">
        <action android:name="android.intent.action.MEDIA_BUTTON"/>
    </intent-filter>
</receiver>

广播接收器文件

public void onReceive(Context context, Intent intent) {
    Log.i("Invisible mode", "Receieved broadcast");
    String intentAction = intent.getAction();
    if(Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){
        KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if(event== null){
            return;
        }

        int keycode = event.getKeyCode();
        int action = event.getAction();
        long eventtime = event.getEventTime();

        if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keycode == KeyEvent.KEYCODE_HEADSETHOOK){
            Log.i("Invisible mode", "Identified media play pause");
            if (action == KeyEvent.ACTION_DOWN){
                Log.i("Invisible mode", "Found action down");
                MainActivity.isInvisibleMode = true;
               Intent main = new Intent();
               main.setClassName("co.za.ss.mn", "co.za.ss.mn.MainActivity");
               main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Log.i("Invisible mode", "Starting main activity in invisible mode");
               context.startActivity(main);
            }
        }
    }
}

而且在我的MainActivity中,我尝试添加了一些代码来注册接收器

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.MEDIA_BUTTON");
registerReceiver(new MediaButtonReceiver(), intentFilter );

((AudioManager) getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(this, MediaButtonReceiver.class));

我在日志中看到了这个警告,而不是我在广播接收器中放置的日志

Cancelling event due to no window focus: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_HEADSETHOOK, scanCode=226, metaState=0, flags=0xa8, repeatCount=0, eventTime=184566337, downTime=184565800, deviceId=1, source=0x101 }

有人可以告诉我哪里出了问题吗?每当我按下耳机按钮时,我的Android 9设备上什么都不会发生。

英文:

I have a problem here, I am attempting to create a Google Assistant sort of a thing in that once you press the headset play or pause button it should get picked up by the broadcaster and start a new activity

Here's what I have so far:

Manifest file

&lt;receiver android:name=&quot;.services.MediaButtonReceiver&quot;&gt;
            &lt;intent-filter android:priority=&quot;2147483647&quot;&gt;
                &lt;action android:name=&quot;android.intent.action.MEDIA_BUTTON&quot;/&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

Broadcast receiver file

public void onReceive(Context context, Intent intent) {
        Log.i(&quot;Invisible mode&quot;, &quot;Receieved broadcast&quot;);
        String intentAction = intent.getAction();
        if(Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){
            KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if(event== null){
                return;
            }

            int keycode = event.getKeyCode();
            int action = event.getAction();
            long eventtime = event.getEventTime();

            if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keycode == KeyEvent.KEYCODE_HEADSETHOOK){
                Log.i(&quot;Invisible mode&quot;, &quot;Identified media play pause&quot;);
                if (action == KeyEvent.ACTION_DOWN){
                    Log.i(&quot;Invisible mode&quot;, &quot;Found action down&quot;);
                    MainActivity.isInvisibleMode = true;
                   Intent main = new Intent();
                   main.setClassName(&quot;co.za.ss.mn&quot;, &quot;co.za.ss.mn.MainActivity&quot;);
                   main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Log.i(&quot;Invisible mode&quot;, &quot;Starting main activity in invisible mode&quot;);
                   context.startActivity(main);
                }
            }
        }
    }

and in my MainActivity I have a tried adding a few lines to register the receiver

IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(&quot;android.intent.action.MEDIA_BUTTON&quot;);
        registerReceiver(new MediaButtonReceiver(), intentFilter );
       ((AudioManager) getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(this, MediaButtonReceiver.class));

I get this warning on the logcat instead of the once I placed in my broadcaster

Cancelling event due to no window focus: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_HEADSETHOOK, scanCode=226, metaState=0, flags=0xa8, repeatCount=0, eventTime=184566337, downTime=184565800, deviceId=1, source=0x101 }

Can anyone show me what I'm doing wrong? Whenever I press the headset button nothing happens using my Android 9 device

答案1

得分: 1

  1. 可以将清单文件中的优先级提高到1000000000000000。
  2. 当您的活动在前台时,可以使用KeyEventListener来监听KEYCODE_MEDIA_PLAY_PAUSE和KEYCODE_HEADSETHOOK。
英文:
  1. Can you increase the priority in manifest file to 1000000000000000
  2. You can use KeyEventListener to listen for KEYCODE_MEDIA_PLAY_PAUSE and KEYCODE_HEADSETHOOK when your activity is in foreground.

huangapple
  • 本文由 发表于 2020年5月4日 06:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61582683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定