我在应用程序中使用windowmanager自定义通知addview。
android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@74b2eff无效;你的活动在运行吗? 在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) 在com.zuck3.petpolar.activity.Inbox.InboxActivity.onCreate(InboxActivity.java:66) 在android.app.Activity.performCreate(Activity.java:6376) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1488) 在android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:207) W / System.err:在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 :(得分:0)
确保您的活动正在运行,请使用
if(mActivity != null && !mActivity.isFinishing())
检查