2016-07-22 38 views
0

我在应用程序中通过使用windowmanager来添加视图的自定义通知。

android.view.WindowManager $ BadTokenException:无法添加窗口 - 标记[email protected]无效;你的活动正在运行? 在android.view.ViewRootImpl.setView(ViewRootImpl.java:685) 在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:319) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 在com.zuck3.petpolar.manager.InboxNotificationManager.showInboxNotification(InboxNotificationManager.java:82) at com.zuck3.petpolar.activity.Inbox.InboxActivity.onCreate(InboxActivity.java:66) at android.app.Activity.performCreate( Activity.java:6376) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) at android.app.ActivityThread.handleLaunchActivity(ActivityThread。 java:2654) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1488) at android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:207) W/System.err:at android.app.ActivityThread.main(ActivityThread.java:5728) W/System.err:at java.lang.reflect .Method.invoke(Native Method) W/System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) W/System.err:at com.android.internal。 os.ZygoteInit.main(ZygoteInit.java:679) W/libEGL:[ANDROID_RECORDABLE]格式:1

InboxNotificationManager.java

public class InboxNotificationManager { 
private static InboxNotificationManager instance; 
private WindowManager mWindowManager; 
private WindowManager.LayoutParams mWindowParams; 
private InboxNotificationView notificationView; 
private Runnable runnable; 
private Handler handler = new Handler(Looper.getMainLooper()); 
private Animation.AnimationListener animationListener = new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     hideInboxNotification(); 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 
}; 


public static InboxNotificationManager Instance(Context context) { 
    if (instance == null) { 
     instance = new InboxNotificationManager(context); 
    } 
    return instance; 

} 

public InboxNotificationManager(Context context) { 
    notificationView = new InboxNotificationView(context); 
    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    mWindowParams = new WindowManager.LayoutParams(); 
    mWindowParams.gravity = Gravity.TOP | Gravity.RIGHT; 
    mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT; 
    mWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT; 
    mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
      | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; 
    mWindowParams.format = PixelFormat.TRANSLUCENT; 
    runnable = new Runnable() { 
     @Override 
     public void run() { 
      notificationView.stopAnimation(animationListener); 

     } 
    }; 


} 

public void setTextMeesage(String meesage) { 
    notificationView.setTextMessage(meesage); 
    // Toast.makeText(InboxNotificationManager.this, "", Toast.LENGTH_SHORT).show(); 


} 

public void showInboxNotification(String message) { 
    if (notificationView.getWindowToken() == null) { 
     mWindowManager.addView(notificationView, mWindowParams); 
     notificationView.startAnimation(); 
     setTextMeesage(message); 

    } else { 
     // mWindowManager.setText(count); 

    } 
    notificationView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      notificationView.stopAnimation(animationListener); 

     } 
    }); 

    handler.removeCallbacks(runnable); 
    handler.postDelayed(runnable, 3000); 


} 

public void hideInboxNotification() { 
    if (notificationView.getWindowToken() != null) { 
     mWindowManager.removeView(notificationView); 
    } 


} 

当我使用这个类

InboxNotificationManager.Instance(BaseActivity.this).showInboxNotification("Hello world"); 

三江源和抱歉,我的英语不好。

+0

你看看在这里的SO此错误相关的其他的答案?我找到了2-3。请分享您的整个'onCreate'方法。 – Vucko

+0

如果您的活动正在完成,并且当时添加了视图,则会引发此异常。因此,在显示视图/对话框之前,请在活动中检查“if(!isFinishing())”。 –

+0

感谢您的回答。但我想显示我的自定义视图,我想要likeToast的每个活动。 –

回答

0

确保您的活动运行,使用

if(mActivity != null && !mActivity.isFinishing()) 

检查

相关问题