如何在触摸

时间:2016-09-09 12:48:32

标签: android service overlay

我想删除在Android服务上添加的TYPE_SYSTEM_OVERLAY视图。我的服务是......

OverlayService服务

    public class OverlayService extends Service {
    WindowManager wm;
    View myView;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.FILL_PARENT, 0, 0,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                        | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                PixelFormat.RGBA_8888);

        wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        myView = inflater.inflate(R.layout.activity_charging, null);


        myView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                System.out.println("==== Remove View ");
                wm.removeView(myView);
                return false;
            }
        });

        wm.addView(myView, params);
        super.onCreate();
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnRemoveView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:text="Remove View" />
</RelativeLayout>

根据上面的代码,当应用程序被调用时,视图会在屏幕上添加,但是按钮单击不起作用,我想要的...我想删除按钮点击时创建的视图,如果可以删除自定义那么TYPE_SYSTEM_OVERLAY怎么样?帮帮我......

感谢。

1 个答案:

答案 0 :(得分:2)

你可能已经修好或继续前进,但以防万一:

它没有回应的原因是因为我无法让TYPE_SYSTEM_OVERLAY响应触摸事件。执行此操作的方法是使用类型TYPE_SYSTEM_ALERT并使用标志:

 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL

然后它应该正确回应你的其余代码。