我想关闭系统音量弹出窗口(用我自己的实现替换它)。系统卷弹出窗口是一个设置了FLAG_WATCH_OUTSIDE_TOUCH
- 标志的对话框。当物理触摸我自己的视图(系统对话框旁边显示)时,系统对话框被正确解除,但调用
myView.performClick();
或
mView.dispatchTouchEvent(motionEvent);
(其中motionEvent
是ACTION_DOWN
- 事件)什么都不做。可以找到系统卷对话框的源here。
答案 0 :(得分:0)
如果只是解除系统音量对话框的问题,下面的代码应该可以工作:
// We will use android.app.Instrumentation to send a MotionEvent
Instrumentation mInstrumentation;
....
....
// We cannot use the main application thread. Note that this is a requirement.
public void dismissSystemDialog() {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
sendMotionEvent();
}
});
t1.start();
}
public void sendMotionEvent() {
if (mInstrumentation == null) {
mInstrumentation = new Instrumentation();
}
// These values should satisfy two requirements:
// 1. They don't map to a point on the System dialog.
// 2. They DO map to a point that is part of your UI.
// Perhaps, your own dialog: use `getLocationOnScreen(int[])`
// for this.
int y = 700;
int x = 50;
final MotionEvent m = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN,
x, y, 1);
try {
mInstrumentation.sendPointerSync(m);
Log.i("", "Dismissing Volume Dialog Now...");
} catch (SecurityException e) {
Log.i("", "Security Exception." +
" Permission `INJECT_EVENTS` is not granted");
}
}
代码不言自明。我希望它能解决你的问题。
答案 1 :(得分:-1)
您可以使用mDialog.dismiss()
或Activity.dismissDialog(int)
自行手动启动对话解雇。