我正在处理来电显示应用程序,我在窗口管理器视图中显示已拨号码/接收号码的信息。用户可以手动关闭视图。一切顺利,直到我在呼叫等待服务上测试它。即使在单击关闭按钮并显示错误“View Not Attached To Window Manager”后,呼叫等待视图仍保持在屏幕上。 我正在服务所有这些东西。
我的问题是,是否有任何方法可以在一次点击中删除多个视图。 或者我可以跟踪窗口管理器附带的视图。
这是我的代码
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();
}
}
});
请帮忙
答案 0 :(得分:1)
使用这个条件:
if (linearLayout.getWindowToken() != null) {
windowManager.removeView(linearLayout);
}