Toasts overlap issue on Oreo (8.1)

若如初见. 提交于 2019-11-30 12:15:08
private static final int TIME_DELAY = 4000;
private static long lastToastShowTime = 0;

showToast(final String msg, final Context ctx){
    final long pastTime = System.currentTimeMillis() - lastToastShowTime;
    if(pastTime > TIME_DELAY ){

        Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
        lastToastShowTime = System.currentTimeMillis();

     }else{
        final long delay = TIME_DELAY - pastTime;
        lastToastShowTime = System.currentTimeMillis() + delay;
        postDelayed(new Runnable(

            @Override
            public void run() {
               try{
                  Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
               catch(Exception e){
                  Log.e("TOAST_NOT_SHOWED", "Toast not showed: " + msg, e);
               }

            }

        ), delay);

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!