WindowManager.removeView()给出了IllegalStateException

时间:2014-09-20 19:25:55

标签: android android-windowmanager

我试图创建一个自定义的chatHead,它只会在特定应用程序打开时显示。 (例如Messenger或WhatsApp)。当应用程序关闭时会被销毁。

这是我的代码 -

@Override
public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);
    chatHead.setImageResource(R.drawable.view_icon);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.START;
    params.x = 0;
    params.y = 100;

    windowManager.addView(chatHead, params);

    getForeground();


    chatHead.setOnClickListener(new View.OnClickListener() {
       /*.....
         .....
         ClickListener implemented
         .....
         .....
       */
    });

}

此处,在onCreate()中,我正在调用getForeground()方法,该方法在前台获取当前活动

public String getForeground() {
    final ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    String foreground = am.getRunningTasks(1).get(0).topActivity.getPackageName();
    if (foreground.equals("com.whatsapp")) {
        if (chatHead==null)
            onResume();
    }
    else
        onDestroy();

    new Thread() {
        @Override
        public void run() {
            getForeground();

        }
    }.start();
    return foreground;
}

其中,onResume()是 -

public void onResume() {
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.START;
    params.x = 0;
    params.y = 100;

    windowManager.addView(chatHead,params);
}

onDestroy()是 -

@Override
public void onDestroy() {
    super.onDestroy();
    if (chatHead != null)
        windowManager.removeView(chatHead);
    else
        chatHead=null;
}

但是,它显示IllegalStateException如下 -

java.lang.IllegalArgumentException: View=android.widget.ImageView{41f07a28 V.ED..C. ......ID 0,0-93,99} not attached to window manager
            at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
            at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
            at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
            at automated.whatsapp.com.automatedtest.ChatHeadService.onDestroy(ChatHeadService.java:172)
            at automated.whatsapp.com.automatedtest.ChatHeadService.getForeground(ChatHeadService.java:188)
            at automated.whatsapp.com.automatedtest.ChatHeadService$3.run(ChatHeadService.java:194)

所以,基本上,它说chatHead没有附加到WindowManager。如何更正此错误?

1 个答案:

答案 0 :(得分:0)

不确定这是否对您有所帮助,但这就是我所做的:

@Override
protected void onUserLeaveHint() {
    if (this.chatHead != null) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ((WindowManager) getApplicationContext().getSystemService(Service.WINDOW_SERVICE)).removeView(chatHead);
            }
        });
    }
}