Android4.0 Toast显示问题分析

在修复RUI桌面在4.0系统下的提示信息不完善的Bug过程的一些思路与大家分享一下。

Bug描述:

RUI在2.2的系统点击推荐图标下载后,就会进入下载队列中下载,如果再次点击相同的图标就会使用Toast提示“**已经在下载队列中”。但是在4.0的系统就会出现异常,第二次点击相同的推荐图标时没有出现Toast提示。

相关源码:

public static void showMessage(final Context act, final int gravity, final String msg) {
        Toast.makeText(act, msg, Toast.LENGTH_SHORT).show();
        new Thread(new Runnable() {
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Log.v(TAG, "showMessage Runnable ThreadID: " + Thread.currentThread());
                        synchronized (synLock) {
                            Log.v(TAG, "showMessage toast text: " + msg);
                            if (toast != null) {
                                Log.v(TAG, "tosat != null");
                                // 修复4.0中推荐下载信息不完善
//                              toast.cancel();
                                toast.setText(msg);
                                toast.setDuration(Toast.LENGTH_SHORT);
                            } else {
                                toast = Toast.makeText(act, msg, Toast.LENGTH_SHORT);
                            }
//                          toast.setGravity(gravity, 0, 0);
                            try{
                                toast.show();
                            } catch(Exception e) {
                                Log.d(TAG, "Exception " + e.getMessage());
                            }
                            Log.v(TAG, "toast: " + toast);
                        }
                    }
                });
            }
        }).start();
    }

分析Bug可能产生的原因:

1.      怀疑是context出现问题的原因。

因为Toast.**makeText(Contextcontext, CharSequencetext, int duration)**中有如下解释:

context The context to use. Usually yourandroid.app.Applicationorandroid.app.Activityobject.

而源码中传入的context是从NetworkService中传入的,所以怀疑这可能是出现Bug的原因。

我将context从Launcher中传入,Toast依然没有出现;直接Toast.makeText(act, msg, Toast.LENGTH_SHORT).show();结果出现Toast提示信息,所以不是context的原因。

2.      怀疑是互斥锁和handler.post()中的原因。

在代码中加入Log信息,从Log打印出来的信息可以得出已经运行到了互斥域内的代码。Log打印出来的运行线程是在主线程中,而且在run的代码中加入Toast.makeText(act, msg, Toast.LENGTH_SHORT),有Toast显示。所以排除了互斥锁和Handler.post()的原因。

经过上述分析后觉得很奇怪,就向其它人请教,让我慢慢调试。

后来看到cancel()的解释如下:

Closethe view if it's showing, or don't show it if it isn't showing yet. You do notnormally have to call this. Normally view will disappear on its own after theappropriate duration.

就怀疑问题出在这里。将代码toast.cancel()注释掉后运行,发现Toast提示信息出现,跟他人汇报后,提示“可以看看4.0中Toast的源码”。

下面是2.2和4.0中Toast源码的链接:

http://www.oschina.net/code/explore/android-2.2-froyo/android/widget/Toast.java

http://www.oschina.net/code/explore/android-4.0.1/core/java/android/widget/Toast.java

比较两者关于cancel()处理的差异发现了如下差异:

在4.0中如下:

final Runnable mHide = new Runnable() {

          public void run() {

               handleHide();

               // Don't do this in handleHide()because it is also invoked by handleShow()

               mNextView = null;

          }

      };

而2.2中没有mNextView = null;这一行代码。

再查看show()中有如下源码:

if (mNextView == null) {

          throw new RuntimeException("setView must have been called");

      }

所以判断产生的Bug的原因是2.2系统和4.0系统对cancel的处理的差异造成的,将cancel注释就可以正常显示了。