如何检测服务中的 keyup 和 keydown 事件
How to detect keyup and keydown event in service
我有一个扩展 LinearLayout
的 CustomView,我使用 WindowManager.addView()
方法将其添加到屏幕,以使用以下布局参数标志检测我服务中的关键事件
LinearLayout mLinear = new LinearLayout(getApplicationContext()) {
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
super.dispatchKeyEvent(event);
Log.i(TAG, "event");
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
Log.i("Key", "VolumeUp");
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
Log.i("Key", "VolumeDown");
return true;
case KeyEvent.KEYCODE_BACK:
Log.d(TAG, " " + "KEYCODE_BACK");
return false;
default:
return true;
}
}
};
mView = LayoutInflater.from(this).inflate(R.layout.volume_service_layout, mLinear);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
if (mView.getVisibility() == View.VISIBLE)
flag = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING ;
//params
params = new WindowManager.LayoutParams(
100,
100,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
wm.addView(mView, params);
}
dispatchKeyEvent()
在 customview
class 中被覆盖,但在将其添加到 windows 后按 Button
而不是 working.When 我正在添加WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
标记 backpress 按钮工作正常,我无法检测到 keyup 和 keydown 事件。
我想在服务 class 中检测 keyup 或 keydown 事件。
请为这个问题提出任何解决方案。
我猜你是通过 BroadcastReceiver
实现的
试试这个,
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"...>
//code, like activities, etc
<receiver android:name="com.example.test.VolumeBroadcast" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</application>
接收者示例:
public class VolumeBroadcast extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
//check the intent something like:
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
// Handle key press.
}
}
}
}
你的注册方式是这样的:
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
在这种情况下,Android 没有记录与 volume buttons
交互的 API。所以我think.Its不可能
要从服务中检测音量键按下尝试 AccessibilityService
并覆盖 onkeyEvent()
然后你在被覆盖的事件中做剩下的计算
我有一个扩展 LinearLayout
的 CustomView,我使用 WindowManager.addView()
方法将其添加到屏幕,以使用以下布局参数标志检测我服务中的关键事件
LinearLayout mLinear = new LinearLayout(getApplicationContext()) {
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
super.dispatchKeyEvent(event);
Log.i(TAG, "event");
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
Log.i("Key", "VolumeUp");
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
Log.i("Key", "VolumeDown");
return true;
case KeyEvent.KEYCODE_BACK:
Log.d(TAG, " " + "KEYCODE_BACK");
return false;
default:
return true;
}
}
};
mView = LayoutInflater.from(this).inflate(R.layout.volume_service_layout, mLinear);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
if (mView.getVisibility() == View.VISIBLE)
flag = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING ;
//params
params = new WindowManager.LayoutParams(
100,
100,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
wm.addView(mView, params);
}
dispatchKeyEvent()
在 customview
class 中被覆盖,但在将其添加到 windows 后按 Button
而不是 working.When 我正在添加WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
标记 backpress 按钮工作正常,我无法检测到 keyup 和 keydown 事件。
我想在服务 class 中检测 keyup 或 keydown 事件。 请为这个问题提出任何解决方案。
我猜你是通过 BroadcastReceiver
实现的试试这个,
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"...>
//code, like activities, etc
<receiver android:name="com.example.test.VolumeBroadcast" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</application>
接收者示例:
public class VolumeBroadcast extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
//check the intent something like:
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
// Handle key press.
}
}
}
}
你的注册方式是这样的:
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
Android 没有记录与 volume buttons
交互的 API。所以我think.Its不可能
要从服务中检测音量键按下尝试 AccessibilityService
并覆盖 onkeyEvent()
然后你在被覆盖的事件中做剩下的计算