为什么 getSystemService(string) 方法只是 Activity class 中的存根?
Why is the getSystemService(string) method just a stub in the Activity class?
我刚刚开始学习 Android,所以我在 Android Studio 中四处闲逛并学习了一些新的键盘快捷键。我对 getSystemService(string)
方法进行了快速定义查找,并将其提取出来:
此方法的来源是否以某种方式隐藏了?它不能真的只是抛出一个 RuntimeException
,对吧......?
它不是存根,但源代码是隐藏的,当您实际调用此方法时可以看到这一点的证据,并且效果理想。
此外,您会在属于 Android Framework
的各种 类 中找到许多此类方法,它们不会公开处理框架的实际逻辑。
如果您想真正了解 getSystemService
的作用,请查看 Github 上的 android 源代码。
这是继承树。
Activity -> ContextThemeWrapper -> Context
在 Context.java
中您可以找到它的实际作用。
/**
* Return the handle to a system-level service by class.
* <p>
* Currently available classes are:
* {@link android.view.WindowManager}, {@link android.view.LayoutInflater},
* {@link android.app.ActivityManager}, {@link android.os.PowerManager},
* {@link android.app.AlarmManager}, {@link android.app.NotificationManager},
* {@link android.app.KeyguardManager}, {@link android.location.LocationManager},
* {@link android.app.SearchManager}, {@link android.os.Vibrator},
* {@link android.net.ConnectivityManager},
* {@link android.net.wifi.WifiManager},
* {@link android.media.AudioManager}, {@link android.media.MediaRouter},
* {@link android.telephony.TelephonyManager}, {@link android.telephony.SubscriptionManager},
* {@link android.view.inputmethod.InputMethodManager},
* {@link android.app.UiModeManager}, {@link android.app.DownloadManager},
* {@link android.os.BatteryManager}, {@link android.app.job.JobScheduler},
* {@link android.app.usage.NetworkStatsManager}.
* </p><p>
* Note: System services obtained via this API may be closely associated with
* the Context in which they are obtained from. In general, do not share the
* service objects between various different contexts (Activities, Applications,
* Services, Providers, etc.)
* </p>
*
* @param serviceClass The class of the desired service.
* @return The service or null if the class is not a supported system service.
*/
@SuppressWarnings("unchecked")
public final <T> T getSystemService(Class<T> serviceClass) {
// Because subclasses may override getSystemService(String) we cannot
// perform a lookup by class alone. We must first map the class to its
// service name then invoke the string-based method.
String serviceName = getSystemServiceName(serviceClass);
return serviceName != null ? (T)getSystemService(serviceName) : null;
}
我刚刚开始学习 Android,所以我在 Android Studio 中四处闲逛并学习了一些新的键盘快捷键。我对 getSystemService(string)
方法进行了快速定义查找,并将其提取出来:
此方法的来源是否以某种方式隐藏了?它不能真的只是抛出一个 RuntimeException
,对吧......?
它不是存根,但源代码是隐藏的,当您实际调用此方法时可以看到这一点的证据,并且效果理想。
此外,您会在属于 Android Framework
的各种 类 中找到许多此类方法,它们不会公开处理框架的实际逻辑。
如果您想真正了解 getSystemService
的作用,请查看 Github 上的 android 源代码。
这是继承树。
Activity -> ContextThemeWrapper -> Context
在 Context.java
中您可以找到它的实际作用。
/**
* Return the handle to a system-level service by class.
* <p>
* Currently available classes are:
* {@link android.view.WindowManager}, {@link android.view.LayoutInflater},
* {@link android.app.ActivityManager}, {@link android.os.PowerManager},
* {@link android.app.AlarmManager}, {@link android.app.NotificationManager},
* {@link android.app.KeyguardManager}, {@link android.location.LocationManager},
* {@link android.app.SearchManager}, {@link android.os.Vibrator},
* {@link android.net.ConnectivityManager},
* {@link android.net.wifi.WifiManager},
* {@link android.media.AudioManager}, {@link android.media.MediaRouter},
* {@link android.telephony.TelephonyManager}, {@link android.telephony.SubscriptionManager},
* {@link android.view.inputmethod.InputMethodManager},
* {@link android.app.UiModeManager}, {@link android.app.DownloadManager},
* {@link android.os.BatteryManager}, {@link android.app.job.JobScheduler},
* {@link android.app.usage.NetworkStatsManager}.
* </p><p>
* Note: System services obtained via this API may be closely associated with
* the Context in which they are obtained from. In general, do not share the
* service objects between various different contexts (Activities, Applications,
* Services, Providers, etc.)
* </p>
*
* @param serviceClass The class of the desired service.
* @return The service or null if the class is not a supported system service.
*/
@SuppressWarnings("unchecked")
public final <T> T getSystemService(Class<T> serviceClass) {
// Because subclasses may override getSystemService(String) we cannot
// perform a lookup by class alone. We must first map the class to its
// service name then invoke the string-based method.
String serviceName = getSystemServiceName(serviceClass);
return serviceName != null ? (T)getSystemService(serviceName) : null;
}