视图未连接到窗口管理器——服务已停止



我的应用程序使用一个服务,通过WindowManager在屏幕上添加一个持久的覆盖:

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      super.onStartCommand(intent, flags, startId); 
         wm.addView(ll, ll_lp);
         addNotification();
    }

用户在服务运行约10-15分钟后收到一个错误,强制关闭应用程序,有以下例外:

 java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:383)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:285)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
at com.MyApp.MyService.onDestroy(MyService.java:210)

下面是MyService中的一行:

@Override
    public void onDestroy() {
        super.onDestroy();
        wm.removeView(ll);
        removeNotification();
    }

我假设发生的事情是Android操作系统正在杀死我的应用程序和WM不能再访问视图ll ?我怎样才能确保

  1. 覆盖持续时间超过10-15分钟,不会被Android OS杀死
  2. 我没有收到这个错误,如果它被杀死

我先删除了通知,然后试图删除视图/捕获一个可能的异常:

try
{
    if(ll != null) wm.removeView(ll);
}
catch(IllegalArgumentException e)
{
    Log.e("myapp", "removed view that is not there");
}

希望这能有所帮助!

最新更新