我正在使用Caller ID应用程序,在该应用程序中,我在窗口管理器视图中显示所拨打/接收的号码的信息。用户可以手动关闭视图。一切顺利,直到我在呼叫等待服务上对其进行了测试。即使在单击关闭按钮并显示错误“视图未附加到窗口管理器”之后,通话中的等待视图仍保留在屏幕上。
我正在通过服务来做所有这些事情。

我的问题是单击是否可以删除多个视图。
或者我可以跟踪附加到窗口管理器的视图。

这是我的代码

linearLayout = new LinearLayout(this);
    linearLayout.addView(textHeaderName);
    linearLayout.addView(textheader);
    linearLayout.addView(chatHead);
    linearLayout.addView(textfooter);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    btnClose.setImageResource(R.drawable.button_close);
    btnClose.setLeft(0);
    chatHead.setBackgroundResource(R.drawable.img);

android.view.WindowManager.LayoutParams layoutparams = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    android.view.WindowManager.LayoutParams layoutparams1 = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    android.view.WindowManager.LayoutParams layoutparams2 = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    final android.view.WindowManager.LayoutParams paramsChatHead = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    android.view.WindowManager.LayoutParams layoutparams3 = new android.view.WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);

    new android.widget.LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);

    layoutparams.gravity = 53;
    layoutparams.y = 0;
    layoutparams1.gravity = 49;
    layoutparams1.x = 0;
    layoutparams1.y = 0;
    layoutparams2.gravity = 49;
    layoutparams2.x = 0;
    layoutparams2.y = 25;
    paramsChatHead.gravity = 49;
    paramsChatHead.x = 0;
    paramsChatHead.y = 50;
    layoutparams3.gravity = 49;
    layoutparams3.x = 0;
    layoutparams3.y = imgheight + 50;

    try
    {
        windowManager.addView(linearLayout, layoutparams1);
        windowManager.addView(btnClose, layoutparams);

    } catch (Exception e)
    {}


结束事件:

btnClose.setOnClickListener(new android.view.View.OnClickListener()
    {
        public void onClick(View view)
        {
            try
            {
                if (linearLayout != null)
                {
                    windowManager.removeView(linearLayout);
                }
                if (btnClose != null)
                {
                    windowManager.removeView(btnClose);
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });


请帮忙

最佳答案

使用这种条件:

 if (linearLayout.getWindowToken() != null) {
                                    windowManager.removeView(linearLayout);
                                }

10-08 03:19