在没有活动的应用中获取Window对象

时间:2017-08-28 09:08:25

标签: android

我有一个Android应用程序,它只包含一个Service和一个View。我没有使用任何活动。

在Service onCreate方法中,View会膨胀,类似于下面的代码块。

@Override
public void onCreate() {
    super.onCreate();
    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    // The floater layout is on top of everything. It does not have a parent.
    floaterView = (FrameLayout) inflater.inflate(R.layout.floaterView, null);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(<set params>);
    windowManager.addView(floaterView, params);
}

我希望获得与此进程相关的Window对象(https://developer.android.com/reference/android/view/Window.html)的引用,但这通常通过Activity.getWindow()完成。 由于我在申请中没有/想要一个活动,这是不可能的。我遇到了View.getWindowToken和View.getWindowId等方法。也许我可以用这些来获取窗口?

我的问题是:如何获得对此过程中创建的Window / Windows对象的引用?

2 个答案:

答案 0 :(得分:0)

您无法获得window服务。如果您想获得当前窗口的widthheight这样的信息,您可以将其获取  显示对象。你可以通过windowManager.getDefaultDisplay()获得它。

从服务添加视图到当前窗口使用windowManager.addView(View view, ViewGroup.LayoutParams params)

要从服务使用中删除视图windowManager.removeViewImmediate(View view)

从服务使用windowManager.updateViewLayout(View view, ViewGroup.LayoutParams params)

更新视图

在此之前,您必须获得窗口叠加权限才能实现此目的。

您可以通过调用此方式获得许可。

Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getActivity().getPackageName()));
            startActivityForResult(intent, REQUEST_PERMISSION_OVERLAY_PICKUP);

希望有所帮助:)

答案 1 :(得分:0)

private void viewDialog() {
        try {

            final View Dialogview = View.inflate(getBaseContext(), R.layout.alert_open_internet, null);
            final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_DIM_BEHIND,
                    PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
            windowManager.addView(Dialogview, params);

            Button btn_cancel = (Button) Dialogview.findViewById(R.id.btn_canceldialog_internetblocked);
            Button btn_okay = (Button) Dialogview.findViewById(R.id.btn_openmainactivity);
            RelativeLayout relativeLayout = (RelativeLayout) Dialogview.findViewById(R.id.rellayout_dialog);
//        windowManager.addView(relativeLayout,params);
            btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if (Dialogview != null) {
//                                ( (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE)).removeView(Dialogview);
                                    windowManager.removeViewImmediate(Dialogview);
                                    windowManager.removeView(Dialogview);
                                }
                            } catch (final IllegalArgumentException e) {
                                e.printStackTrace();
                                // Handle or log or ignore
                            } catch (final Exception e) {
                                e.printStackTrace();
                                // Handle or log or ignore
                            } finally {
                                try {
                                    if (windowManager != null && Dialogview != null) {
//                                    ((WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE)).removeView(Dialogview);
                                        windowManager.removeViewImmediate(Dialogview);
                                        windowManager.removeView(Dialogview);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            //    ((WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE)).removeView(Dialogview);
//                        windowManager.removeView(Dialogview);


                        }
                    });
                }
            });
            btn_okay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //        ((WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE)).removeView(Dialogview);
                            try {
                                if (windowManager != null && Dialogview != null) {
                                    windowManager.removeView(Dialogview);
                                }
                                Intent intent = new Intent(getBaseContext(), SplashActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(intent);
                            } catch (Exception e) {
                                windowManager.removeViewImmediate(Dialogview);
                                windowManager.removeView(Dialogview);
                                e.printStackTrace();
                            }
                        }
                    });
                }
            });
        } catch (Exception e) {
            //` windowManager.removeView(Dialogview);
            e.printStackTrace();
        }
    }

在服务处理程序中调用上面的方法,否则会导致崩溃

Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        viewDialog();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });