2013-02-16 80 views
1

我試圖從grepcode site追蹤AOSP代碼。當我從我的活動中調用getSystemService()時,哪些代碼正在運行?

當我打電話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";,如果其中一個條件不會下降,會去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); 
    } 

在這裏,所需的服務名稱也不匹配,mBaseContext的一個實例,因此Context中的代碼是:

public abstract Object getSystemService(String name); 

這意味着從它擴展的類必須處理該功能。 那麼,我的請求正在接受治療?

+2

在grepcode.com上,在'Object'和'getSystemService('有一個向下的箭頭,單擊它並選擇「查找派生方法「。它會帶你進入界面的實現。 – Sam 2013-02-16 20:29:03

回答

2

據我所知上下文的實現代碼與類名ContextImpl包android.app

這裏是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); 
     }}); 
相關問題