gpt4 book ai didi

android - 屏幕锁定时如何获取音量键事件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:39 25 4
gpt4 key购买 nike

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}

此代码在屏幕打开时有效。但不是在屏幕锁定时。有没有办法在屏幕锁定时获取音量键事件?

最佳答案

事件仅在屏幕打开时触发。否则需要 root 权限。

Make a BroadcastReceiver extended class

 public class YourBoardcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {


Log.e("get something", "i dont know what!!");

String intentAction = intent.getAction();
KeyEvent event = null;
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
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) {
if (action == KeyEvent.ACTION_DOWN) {
// Start your app here!

// ...
Log.e("event/////", "Trigerd");

if (isOrderedBroadcast()) {
abortBroadcast();
}
}
}
}
}
}

And in your Manifest :

<receiver android:name="YourBoardcastReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>

and Call it like this.

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mRemoteControlResponder = new ComponentName(getPackageName(),
YourBoardcastReceiver.class.getName());

关于android - 屏幕锁定时如何获取音量键事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16144259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com