Compartilhar via


Context.GetSystemService Method

Definition

Overloads

GetSystemService(Class)

Return the handle to a system-level service by class.

GetSystemService(String)

Return the handle to a system-level service by name.

GetSystemService(Class)

Return the handle to a system-level service by class.

[Android.Runtime.Register("getSystemService", "(Ljava/lang/Class;)Ljava/lang/Object;", "", ApiSince=23)]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public Java.Lang.Object? GetSystemService (Java.Lang.Class serviceClass);
[<Android.Runtime.Register("getSystemService", "(Ljava/lang/Class;)Ljava/lang/Object;", "", ApiSince=23)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
member this.GetSystemService : Java.Lang.Class -> Java.Lang.Object

Parameters

serviceClass
Class

The class of the desired service.

Returns

The service or null if the class is not a supported system service. Note: <b>never</b> throw a RuntimeException if the name is not supported.

Attributes

Remarks

Return the handle to a system-level service by class.

Currently available classes are: android.view.WindowManager, android.view.LayoutInflater, android.app.ActivityManager, android.os.PowerManager, android.app.AlarmManager, android.app.NotificationManager, android.app.KeyguardManager, android.location.LocationManager, android.app.SearchManager, android.os.Vibrator, android.net.ConnectivityManager, android.net.wifi.WifiManager, android.media.AudioManager, android.media.MediaRouter, android.telephony.TelephonyManager, android.telephony.SubscriptionManager, android.view.inputmethod.InputMethodManager, android.app.UiModeManager, android.app.DownloadManager, android.os.BatteryManager, android.app.job.JobScheduler, android.app.usage.NetworkStatsManager, android.content.pm.verify.domain.DomainVerificationManager, android.view.displayhash.DisplayHashManager.

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.)

Note: Instant apps, for which PackageManager#isInstantApp() returns true, don't have access to the following system services: #DEVICE_POLICY_SERVICE, #FINGERPRINT_SERVICE, #KEYGUARD_SERVICE, #SHORTCUT_SERVICE, #USB_SERVICE, #WALLPAPER_SERVICE, #WIFI_P2P_SERVICE, #WIFI_SERVICE, #WIFI_AWARE_SERVICE. For these services this method will return null. Generally, if you are running as an instant app you should always check whether the result of this method is null.

Java documentation for android.content.Context.getSystemService(java.lang.Class<T>).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

GetSystemService(String)

Return the handle to a system-level service by name.

[Android.Runtime.Register("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", "GetGetSystemService_Ljava_lang_String_Handler")]
public abstract Java.Lang.Object? GetSystemService (string name);
[<Android.Runtime.Register("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", "GetGetSystemService_Ljava_lang_String_Handler")>]
abstract member GetSystemService : string -> Java.Lang.Object

Parameters

name
String

The name of the desired service.

Returns

The service or null if the name does not exist.

Attributes

Remarks

Return the handle to a system-level service by name. The class of the returned object varies by the requested name. Currently available names are:

<dl> <dt> #WINDOW_SERVICE ("window") <dd> The top-level window manager in which you can place custom windows. The returned object is a android.view.WindowManager. Must only be obtained from a visual context such as Activity or a Context created with #createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen. <dt> #LAYOUT_INFLATER_SERVICE ("layout_inflater") <dd> A android.view.LayoutInflater for inflating layout resources in this context. Must only be obtained from a visual context such as Activity or a Context created with #createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen. <dt> #ACTIVITY_SERVICE ("activity") <dd> A android.app.ActivityManager for interacting with the global activity state of the system. <dt> #WALLPAPER_SERVICE ("wallpaper") <dd> A android.service.wallpaper.WallpaperService for accessing wallpapers in this context. Must only be obtained from a visual context such as Activity or a Context created with #createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen. <dt> #POWER_SERVICE ("power") <dd> A android.os.PowerManager for controlling power management. <dt> #ALARM_SERVICE ("alarm") <dd> A android.app.AlarmManager for receiving intents at the time of your choosing. <dt> #NOTIFICATION_SERVICE ("notification") <dd> A android.app.NotificationManager for informing the user of background events. <dt> #KEYGUARD_SERVICE ("keyguard") <dd> A android.app.KeyguardManager for controlling keyguard. <dt> #LOCATION_SERVICE ("location") <dd> A android.location.LocationManager for controlling location (e.g., GPS) updates. <dt> #SEARCH_SERVICE ("search") <dd> A android.app.SearchManager for handling search. <dt> #VIBRATOR_MANAGER_SERVICE ("vibrator_manager") <dd> A android.os.VibratorManager for accessing the device vibrators, interacting with individual ones and playing synchronized effects on multiple vibrators. <dt> #VIBRATOR_SERVICE ("vibrator") <dd> A android.os.Vibrator for interacting with the vibrator hardware. <dt> #CONNECTIVITY_SERVICE ("connectivity") <dd> A android.net.ConnectivityManager ConnectivityManager for handling management of network connections. <dt> #IPSEC_SERVICE ("ipsec") <dd> A android.net.IpSecManager IpSecManager for managing IPSec on sockets and networks. <dt> #WIFI_SERVICE ("wifi") <dd> A android.net.wifi.WifiManager WifiManager for management of Wi-Fi connectivity. On releases before Android 7, it should only be obtained from an application context, and not from any other derived context to avoid memory leaks within the calling process. <dt> #WIFI_AWARE_SERVICE ("wifiaware") <dd> A android.net.wifi.aware.WifiAwareManager WifiAwareManager for management of Wi-Fi Aware discovery and connectivity. <dt> #WIFI_P2P_SERVICE ("wifip2p") <dd> A android.net.wifi.p2p.WifiP2pManager WifiP2pManager for management of Wi-Fi Direct connectivity. <dt> #INPUT_METHOD_SERVICE ("input_method") <dd> An android.view.inputmethod.InputMethodManager InputMethodManager for management of input methods. <dt> #UI_MODE_SERVICE ("uimode") <dd> An android.app.UiModeManager for controlling UI modes. <dt> #DOWNLOAD_SERVICE ("download") <dd> A android.app.DownloadManager for requesting HTTP downloads <dt> #BATTERY_SERVICE ("batterymanager") <dd> A android.os.BatteryManager for managing battery state <dt> #JOB_SCHEDULER_SERVICE ("taskmanager") <dd> A android.app.job.JobScheduler for managing scheduled tasks <dt> #NETWORK_STATS_SERVICE ("netstats") <dd> A android.app.usage.NetworkStatsManager NetworkStatsManager for querying network usage statistics. <dt> #HARDWARE_PROPERTIES_SERVICE ("hardware_properties") <dd> A android.os.HardwarePropertiesManager for accessing hardware properties. <dt> #DOMAIN_VERIFICATION_SERVICE ("domain_verification") <dd> A android.content.pm.verify.domain.DomainVerificationManager for accessing web domain approval state. <dt> #DISPLAY_HASH_SERVICE ("display_hash") <dd> A android.view.displayhash.DisplayHashManager for management of display hashes. </dl>

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.)

Note: Instant apps, for which PackageManager#isInstantApp() returns true, don't have access to the following system services: #DEVICE_POLICY_SERVICE, #FINGERPRINT_SERVICE, #KEYGUARD_SERVICE, #SHORTCUT_SERVICE, #USB_SERVICE, #WALLPAPER_SERVICE, #WIFI_P2P_SERVICE, #WIFI_SERVICE, #WIFI_AWARE_SERVICE. For these services this method will return null. Generally, if you are running as an instant app you should always check whether the result of this method is null.

Note: When implementing this method, keep in mind that new services can be added on newer Android releases, so if you're looking for just the explicit names mentioned above, make sure to return null when you don't recognize the name &mdash; if you throw a RuntimeException exception instead, your app might break on new Android releases.

Java documentation for android.content.Context.getSystemService(java.lang.String).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to