英文:
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
<receiver android:name=".services.MediaButtonReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
Broadcast receiver file
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);
}
}
}
}
and in my MainActivity I have a tried adding a few lines to register the receiver
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));
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
- 可以将清单文件中的优先级提高到1000000000000000。
- 当您的活动在前台时,可以使用KeyEventListener来监听KEYCODE_MEDIA_PLAY_PAUSE和KEYCODE_HEADSETHOOK。
英文:
- Can you increase the priority in manifest file to 1000000000000000
- You can use KeyEventListener to listen for KEYCODE_MEDIA_PLAY_PAUSE and KEYCODE_HEADSETHOOK when your activity is in foreground.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论