I custom notification in application by use windowmanager to addview.
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@74b2eff is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:685) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:319) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at 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) at 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] format: 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);
}
}
And when I use this class
InboxNotificationManager.Instance(BaseActivity.this).showInboxNotification("Hello world");
Thankyou and sorry my bad english.
onCreate
method please. – Vuckoif(!isFinishing())
in activity. – Akshay Bhat 'AB'