安卓提示框不会消失

11

我展示了提示,但即使在应用关闭后它也不消失。该如何修复?

@Override
public void onClipStoreLoadedClipsNotification(ClipStoreLoadedClipsNotification notif) 
{
    final ClipStoreLoadedClipsNotification notification = notif;

    runOnUiThread(new Runnable() {
        @Override
        public void run() 
        {
            Dialogs.DismissAll();
            list.onRefreshComplete();
            TextView text = (TextView)findViewById(R.id.loadclipstext);
            ProgressBar pb = (ProgressBar)findViewById(R.id.loadclipsprogress);

            if (notification.moreClipsAvailable)
            {
                text.setText(context.getString(R.string.loading_clips));
                pb.setVisibility(View.VISIBLE);
            }
            else
            {
                text.setText(context.getString(R.string.no_clips));
                pb.setVisibility(View.INVISIBLE);
                int duration = Toast.LENGTH_SHORT;
                Toast.makeText(SugarLoafContext.playbackTabContext, "No clips found.", duration).show();
            }

            SugarLoafContext.currentCamera = notification.camera;
            clipList = notification.clips;

            refreshListView();
            readyToLoadMoreClips = true;
            if (!firstClipsLoaded)
                firstClipsLoaded = true;
        }
    });


}

设置时间间隔的方式如下:int duration = Toast.LENGTH_SHORT; - Harsha M V
3个回答

48

它是否在IntentService中运行?

如果是的话,问题就出在Android中的Intent服务在不同的线程中运行,因此Toast会在不同的线程中显示,之后在显示时间结束后,Android系统无法找到Toast所在的位置,因此无法将其移除。

我曾经遇到过同样的问题,现在我建议大家不要在IntentService中显示Toast,而是尝试运行一个普通的服务或打开一个活动,或者如果完全必要显示Toast,则尝试其他方法。

当您关闭应用程序时Toast没有消失的原因是IntentService仍在运行,您必须重新启动系统或卸载应用程序才能使Intent Service关闭。


这对我来说不是什么问题,看到后台有东西在运行,我感到非常兴奋 :-) - Nabin
我也遇到了一些问题,我也在使用IntentService。这个问题有解决方案吗? - sandeepmaaram
启动一个Activity,并让该Activity成为启动Toast的Activity。我知道这很困难,但我们必须理解IntentService不在UI线程上工作,因此我们必须启动一个Activity(使用NEW_TASK标志),并让该Activity启动Toast。如果您想,可以将Activity作为对用户不可见的方式启动,以免出现任何奇怪的情况。否则,您还可以查看Snackbar元素:https://developer.android.com/reference/android/support/design/widget/Snackbar.html - zapotec

8
唯一的解释是你的Toast被循环调用。你应该跟踪toast.show(),看它是否被无限次调用。
另一种可视化的方法是这样做。
 Toast.makeText(SugarLoafContext.playbackTabContext, "No clips found.", duration).show();
 Toast.makeText(SugarLoafContext.playbackTabContext, "Do you understand now?", duration).show();

我相信你会在很长一段时间内看到两个提示交替出现...

0
如果您的设备没有网络连接,并且您的应用程序进行许可证检查并显示结果的 Toast,则也可能发生这种情况。我的一个应用程序就遇到了这个问题,因为我在检查应用程序是否已获得许可证时显示了一个 Toast。没有网络连接时,告诉用户要进行许可证重试的 Toast 仍然存在,当我连接后,Toast 被移除,因为许可证检查可以工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接