2011-09-06 104 views
9

即使在应用程序关闭后,我也会显示吐司,它不会消失。我该如何解决?Android吐司不会消失

@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; 
     } 
    }); 


} 
+0

像这样设置持续时间int duration = Toast.LENGTH_SHORT; –

回答

7

唯一的解释是您的Toast在循环中被调用。你应该跟踪toast .show(),看看它是否被称为无限次。

的其他可视化的方式将做到这一点

Toast.makeText(SugarLoafContext.playbackTabContext, "No clips found.", duration).show(); 
Toast.makeText(SugarLoafContext.playbackTabContext, "Do you understand now?", duration).show(); 

我相信你将在一个looong时间或者看到两个面包......

44

它是一个IntentService内运行???

如果是这样,问题在于Android中的Intent Services运行在与主节点不同的线程中,所以Toast在与主节点不同的线程中显示,在显示时间结束后, Android系统无法找到Toast在哪里,所以它不能删除它。

我有同样的问题,现在,我建议大家不要在IntentService的内部显示Toast,而是尝试运行一个通用服务,或者打开一个活动,或尝试一些不同的东西,如果完全有必要显示吐司。

当您关闭应用程序时Toast不会消失的事实是IntentService仍在运行,您必须重新启动系统或卸载应用程序以使Intent Service关闭。

+0

这对我来说不是一个问题,我很兴奋地看到一些在后台运行的东西:-) – Nabin

+0

对于我也面临一些问题,同样我也使用IntentService。有没有解决方案? – sandeepmaaram

+0

启动一个活动,并让该活动成为启动吐司的活动。我知道这很难,但我们必须了解IntentService不能在UI线程上工作,所以我们必须启动一个Activity(使用NEW_TASK标志,并让Activity启动Toast)。如果你愿意,你可以启动这个活动,因为用户不会看到任何奇怪的东西。否则,您也可以检查Snackbar元素:https://developer.android.com/reference/android/support/design/widget/Snackbar.html – zapotec

0

如果您的设备没有网络连接,并且您的应用执行了许可检查并显示Toast结果,也会发生这种情况。我的一个应用程序有这个问题,因为我在检查应用程序时显示Toast。如果没有网络连接,Toast会告诉用户仍然需要执行许可证重试,并且当我连接Toast时被移除,因为许可证检查可能有效。