跟踪添加到 WindowManager 的视图(没有 findViewById() 函数?) [英] Keeping track of View added to WindowManager (no findViewById() function?)

查看:20
本文介绍了跟踪添加到 WindowManager 的视图(没有 findViewById() 函数?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务中,我使用 addView()WindowManager 添加了一个视图.当我准备隐藏视图时,我使用 View 引用调用 removeView().这很有效 - 大多数情况下.

我偶尔有强制关闭报告说视图没有附加到 WindowManager.这是有道理的.问题是我认为该服务被 Android 杀死了,当需要隐藏视图时,它会尝试在错误的 View 上删除视图.

我曾尝试检查 View 是否为空,但显然此时不是,它根本不是附加到 WindowManager 的那个.看来,如果 View 引用丢失了,就没有办法再次访问它了.

如何在 WindowManager 本身上获得与 findViewById() 等效的内容?如果我的服务停止(终止),View 是否会自动从 WindowManager 中删除?有没有一种方法可以存储对 View 的引用,这样如果服务停止,我仍然可以稍后删除 View(我也试图避免泄漏查看)?

解决方案

在我的服务中,我使用 addView() 向 WindowManager 添加了一个视图.当我准备隐藏视图时,我使用 View 引用调用 removeView().这在大多数情况下效果很好.我偶尔有 Force Close 报告说 View 没有附加到 WindowManager.

我遇到了完全相同的问题.希望有专家指点一下.

<块引用>

如何在 WindowManager 本身上获得等效的 findViewById()?

保留对添加的视图的引用,只需使用 removeView(mView).

当前在我的服务中,添加视图:

WindowManager.LayoutParams params = ...mView = new View(this);WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);wm.addView(mView, params);

然后为了删除视图,我通过捕获异常来避免偶尔的 FC:

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);尝试 {wm.removeView(mView);} 捕获(异常 e){}

<块引用>

如果我的服务停止(终止),视图是否会自动从 WindowManager 中删除?

根据我的经验,当服务被终止时,它们会被删除.您可以通过停止服务而不删除视图来测试这一点.不知道为什么会这样.

我担心的是,当我删除视图时,我对 WM 的第二次引用是不同的.如果是这种情况,当我捕捉到异常时,是否仍然显示 mView?我会在添加视图时尝试保留对 WM 的引用,但我遇到了一些问题,对系统服务的引用似乎随着时间的推移而变坏.

如果你解决了这个问题,请告诉我.

In my service I add a view to WindowManager with addView(). When I'm ready to hide the view, I call removeView() using the View reference. This works great -- most of the time.

I have occasional Force Close reports that say that the View is not attached to the WindowManager. This makes sense. The problem is that I think the service is getting killed by Android, and when it is time to hide the view, it attempts to removeView on the wrong View.

I have tried checking for the View to be null, but apparently it isn't at this point, it simply isn't the one attached to the WindowManager. It seems that if the View reference is lost, there is no way to gain access to it again.

How can I get the equivalent of findViewById() on the WindowManager itself? Is the View automatically removed from WindowManager if my service is stopped (killed)? Is there a way I can store the reference to the View so that if the service is stopped I can still remove the View later (I'm also trying to avoid leaking the View)?

解决方案

In my service I add a view to WindowManager with addView(). When I'm ready to hide the view, I call removeView() using the View reference. This works great--most of the time. I have occasional Force Close reports that say that the View is not attached to the WindowManager.

I am having the exact same problem. Hopefully an expert will chime in.

How can I get the equivalent of findViewById() on the WindowManager itself?

Keep a reference to the View that was added and simply use removeView(mView).

Currently in my Service, to add the view:

WindowManager.LayoutParams params = ...
mView = new View(this);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);

Then to remove the view, I avoid occasional FC by catching the exception:

WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
try {
    wm.removeView(mView);
} catch (Exception e) {}

Is the View automatically removed from WindowManager if my service is stopped (killed)?

In my experience, they are removed when the service is killed. You can test this by stopping your service without removing the views. No idea why that works though.

My concern is that my second reference to the WM is different when I go to remove the view. If that is the case, is mView still being displayed when I catch the exception? I would try keeping a reference to the WM when I add the view, but I've had issues where references to System Services seem to go bad over time.

Let me know if you ever solved this problem.

这篇关于跟踪添加到 WindowManager 的视图(没有 findViewById() 函数?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆