我在应用程序中使用windowmanager自定义通知addview .
android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@74b2eff无效;你的活动在运行吗?在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:319)上的android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)的android.view.ViewRootImpl.setView(ViewRootImpl.java:685)位于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)在android.app.Anstrumentation.callActivityOnCreate(Instrumentation.java:1113)的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)在Android.app的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) . android.app.Loper.loop上的android.app.AandThread(ActivityThread.java:1488)的android.O.Handler.dispatchMessage(Handler.java:111)上的ActivityThread.-wrap11(ActivityThread.java) Looper.java:207)W / System.err:在android.app.ActivityThread.main(ActivityThread.java:5728)W / Syst em.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");
谢谢你,对不起,我的英文不好 .
1 回答
确保您的活动正在运行,使用
去检查