1

我正在尝试从grepcode 站点跟踪 AOSP 代码。

当我打电话时getSystemService(Context.WIFI_P2P_SERVICE),它会得到以下代码:

 @Override public Object getSystemService(String name) {
    if (getBaseContext() == null) {
        throw new IllegalStateException(
                "System services not available to Activities before onCreate()");
    }
    if (WINDOW_SERVICE.equals(name)) {
        return mWindowManager;
    } else if (SEARCH_SERVICE.equals(name)) {
        ensureSearchManager();
        return mSearchManager;
    }
    return super.getSystemService(name);
}

并且由于WIFI_P2P_SERVICE声明为public static final String WIFI_P2P_SERVICE = "wifip2p";, if 将不属于其中一个条件,并将转到super.getSystemService(name);

Activity extends ContextThemeWrapper, the code there is:
 @Override public Object getSystemService(String name) {
        if (LAYOUT_INFLATER_SERVICE.equals(name)) {
            if (mInflater == null) {
                mInflater = LayoutInflater.from(mBase).cloneInContext(this);
            }
            return mInflater;
        }
        return mBase.getSystemService(name);
    }

在这里,所需的服务名称也将不匹配,mBase是一个实例,Context因此 Context 中的代码是:

public abstract Object getSystemService(String name);

这意味着从它扩展的类必须处理该功能。 那么,我的请求在哪里得到处理?

4

1 回答 1

2

据我所知 Context 的实现代码在 android.app 包下,类名为 ContextImpl

这是该类的 getSystemService -

@Override
public Object getSystemService(String name) {
    ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
    return fetcher == null ? null : fetcher.getService(this);
}

编辑 - WIFI_P2P_SERVICE 的入口点 -

registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
    public Object createService(ContextImpl ctx) {
          IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
          IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
          return new WifiP2pManager(service);
        }});
于 2013-02-16T20:20:01.527 回答