Android 一些没有关注过的类 ,陆续更新。。。

最近看Android应用源码,发现了一些没怎么关注的类,在这里记录下:

1、ContextWrapper,继承关系如下

      java.lang.Object
        android.content.Context
            android.content.ContextWrapper
                android.view.ContextThemeWrapper
                    android.app.Activity

它原来是用来对Context进行包装,从而生成另外一个Context,对这个Context修改不会对原来的Context造成影响。

以下是Music模块的代码中用到的。

public static ServiceToken bindToService(Activity context, ServiceConnection callback) {
        Activity realActivity = context.getParent();
        if (realActivity == null) {
            realActivity = context;
        }
        ContextWrapper cw = new ContextWrapper(realActivity);
        cw.startService(new Intent(cw, MediaPlaybackService.class));
        ServiceBinder sb = new ServiceBinder(callback);
        if (cw.bindService((new Intent()).setClass(cw, MediaPlaybackService.class), sb, 0)) {
            sConnectionMap.put(cw, sb);
            return new ServiceToken(cw);
        }
        Log.e("Music", "Failed to bind to service");
        return null;
    }



猜你喜欢

转载自blog.csdn.net/ngyzqf/article/details/45498369