追記
AndroidXから `ContextCompat.getSystemService(context:Context, class:Class)`が可能になった。
Castも不要なので取得可能なクラスが分かっている場合はこちらの方が楽だろう。
val am = ContextCompat.getSystemService(this, ActivityManager::class.java)
元記事
Context.getSystemService("XXX")で引数にできるサービスの一覧。
どうして最初からメソッドになってないのだろう。
WindowManager | WINDOW_SERVICE |
LayoutInflater | LAYOUT_INFLATER_SERVICE |
ActivityManager | ACTIVITY_SERVICE |
PowerManager | POWER_SERVICE |
AlarmManager | ALARM_SERVICE |
NotificationManager | NOTIFICATION_SERVICE |
KeyguardManager | KEYGUARD_SERVICE |
LocationManager | LOCATION_SERVICE |
SearchManager | SEARCH_SERVICE |
SensorManager | SENSOR_SERVICE |
StorageManager | STORAGE_SERVICE |
Vibrator | VIBRATOR_SERVICE |
ConnectivityManager | CONNECTIVITY_SERVICE |
WifiManager | WIFI_SERVICE |
AudioManager | AUDIO_SERVICE |
MediaRouter | MEDIA_ROUTER_SERVICE |
TelephonyManager | TELEPHONY_SERVICE |
InputMethodManager | INPUT_METHOD_SERVICE |
UiModeManager | UI_MODE_SERVICE |
DownloadManager | DOWNLOAD_SERVICE |
上記一覧を一括置換してUtilクラスを適当に作ってみた。
public class ManagerUtil { public static WindowManager getWindowManager(Context context) { return (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } public static LayoutInflater getLayoutInflater(Context context) { return (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public static ActivityManager getActivityManager(Context context) { return (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); } public static PowerManager getPowerManager(Context context) { return (PowerManager) context.getSystemService(Context.POWER_SERVICE); } public static AlarmManager getAlarmManager(Context context) { return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); } public static NotificationManager getNotificationManager(Context context) { return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); } public static KeyguardManager getKeyguardManager(Context context) { return (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); } public static LocationManager getLocationManager(Context context) { return (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); } public static SearchManager getSearchManager(Context context) { return (SearchManager) context.getSystemService(Context.SEARCH_SERVICE); } public static SensorManager getSensorManager(Context context) { return (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); } public static StorageManager getStorageManager(Context context) { return (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); } public static Vibrator getVibrator(Context context) { return (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); } public static ConnectivityManager getConnectivityManager(Context context) { return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); } public static WifiManager getWifiManager(Context context) { return (WifiManager) context.getSystemService(Context.WIFI_SERVICE); } public static AudioManager getAudioManager(Context context) { return (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public static MediaRouter getMediaRouter(Context context) { return (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE); } public static TelephonyManager getTelephonyManager(Context context) { return (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); } public static InputMethodManager getInputMethodManager(Context context) { return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); } public static UiModeManager getUiModeManager(Context context) { return (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE); } public static DownloadManager getDownloadManager(Context context) { return (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); } }