强制OUTSIDE_TOUCH事件?

时间:2014-04-23 11:49:01

标签: android

我想关闭系统音量弹出窗口(用我自己的实现替换它)。系统卷弹出窗口是一个设置了FLAG_WATCH_OUTSIDE_TOUCH - 标志的对话框。当物理触摸我自己的视图(系统对话框旁边显示)时,系统对话框被正确解除,但调用

myView.performClick();

mView.dispatchTouchEvent(motionEvent);

(其中motionEventACTION_DOWN - 事件)什么都不做。可以找到系统卷对话框的源here

2 个答案:

答案 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)自行手动启动对话解雇。