首页 > 代码库 > android源代码分析 android toast使用具体解释 toast自己定义

android源代码分析 android toast使用具体解释 toast自己定义

在安卓开发过程中。toast使我们常常使用的一个类。当我们须要向用户传达一些信息,可是不须要和用户交互时,该方式就是一种十分恰当的途径。

我们习惯了这样使用toast:Toast.makeText(Context context, String info, int duration).show();该方法是

系统为我们提供的一个方便的创建toast对象的静态方法,其内部依旧是调用toast的相关方法完毕。以下

就从其源代码对该类的实现做一个分析

在toast类中,最重要的用于显示该toast的show方法调用了service.enqueueToast(pkg, tn, mDuration);也就是说

系统为我们维持了一个toast队列,这也是为什么两个toast不会同一时候显示的原因。该方法将一个toast入队,显示则由系统维持显示的时机。

 private static INotificationManager sService;


    static private INotificationManager getService() {
        if (sService != null) {
            return sService;
        }
        sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));
        return sService;
    }
该服务sService就是系统用于维护toast的服务。

在toast内部又一个静态私有类TN,该类是toast的主要实现,该类完毕了toast视图的创建,等等


fa

android源代码分析 android toast使用具体解释 toast自己定义