public class Managers
extends java.lang.Object
getSystemService(Class)
method to the Context
class.
System service classes added after Android API 23 are not supported by this
class; if you are using a system service added after API 23, then you should
consider this class deprecated and use the new method in Context
instead.
For backward compatibility from Android API 14 onward, the class provides
constant values for all static manager tags that were added to
Context
after API 14.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
APP_OPS_SERVICE
Backward-compatibility constant for
Context.APP_OPS_SERVICE
(added in Android API 19) |
static java.lang.String |
APPWIDGET_SERVICE
Backward-compatibility constant for
Context.APPWIDGET_SERVICE
(added in Android API 21) |
static java.lang.String |
BATTERY_SERVICE
Backward-compatibility constant for
Context.BATTERY_SERVICE
(added in Android API 21) |
static java.lang.String |
BLUETOOTH_SERVICE
Backward-compatibility constant for
Context.BLUETOOTH_SERVICE
(added in Android API 18) |
static java.lang.String |
CAMERA_SERVICE
Backward-compatibility constant for
Context.CAMERA_SERVICE
(added in Android API 21) |
static java.lang.String |
CAPTIONING_SERVICE
Backward-compatibility constant for
Context.CAPTIONING_SERVICE
(added in Android API 19) |
static java.lang.String |
CONSUMER_IR_SERVICE
Backward-compatibility constant for
Context.CONSUMER_IR_SERVICE
(added in Android API 19) |
static java.lang.String |
DISPLAY_SERVICE
Backward-compatibility constant for
Context.DISPLAY_SERVICE
(added in Android API 17) |
static java.lang.String |
INPUT_SERVICE
Backward-compatibility constant for
Context.INPUT_SERVICE
(added in Android API 16) |
static java.lang.String |
JOB_SCHEDULER_SERVICE
Backward-compatibility constant for
Context.JOB_SCHEDULER_SERVICE
(added in Android API 21) |
static java.lang.String |
LAUNCHER_APPS_SERVICE
Backward-compatibility constant for
Context.LAUNCHER_APPS_SERVICE
(added in Android API 21) |
protected static java.lang.String |
LOG_TAG |
static java.lang.String |
MEDIA_PROJECTION_SERVICE
Backward-compatibility constant for
Context.MEDIA_PROJECTION_SERVICE
(added in Android API 21) |
static java.lang.String |
MEDIA_ROUTER_SERVICE
Backward-compatibility constant for
Context.MEDIA_ROUTER_SERVICE
(added in Android API 16) |
static java.lang.String |
MEDIA_SESSION_SERVICE
Backward-compatibility constant for
Context.MEDIA_SESSION_SERVICE
(added in Android API 21) |
static java.lang.String |
NSD_SERVICE
Backward-compatibility constant for
Context.NSD_SERVICE
(added in Android API 16) |
static java.lang.String |
PRINT_SERVICE
Backward-compatibility constant for
Context.PRINT_SERVICE
(added in Android API 19) |
static java.lang.String |
RESTRICTIONS_SERVICE
Backward-compatibility constant for
Context.RESTRICTIONS_SERVICE
(added in Android API 21) |
static java.util.HashMap<java.lang.Class<?>,java.lang.String> |
REVERSE_MAP
Defines a reverse mapping from class definitions to the constants by
which they are identified in
Context . |
static java.lang.String |
TELECOM_SERVICE
Backward-compatibility constant for
Context.TELECOM_SERVICE
(added in Android API 21) |
static java.lang.String |
TELEPHONY_SUBSCRIPTION_SERVICE
Backward-compatibility constant for
Context.TELECOM_SUBSCRIPTION_SERVICE
(added in Android API 22) |
static java.lang.String |
TV_INPUT_SERVICE
Backward-compatibility constant for
Context.TV_INPUT_SERVICE
(added in Android API 21) |
static java.lang.String |
USAGE_STATS_SERVICE
Backward-compatibility constant for
Context.USAGE_STATS_SERVICE
(added in Android API 22) |
static java.lang.String |
USER_SERVICE
Backward-compatibility constant for
Context.USER_SERVICE
(added in Android API 17) |
Constructor and Description |
---|
Managers() |
Modifier and Type | Method and Description |
---|---|
static <MGR> MGR |
get(android.content.Context ctx,
java.lang.Class<MGR> cls)
Obtains an instance of the Android OS service whose class is given.
|
protected static void |
initReverseMap(int nVersion)
Initializes the reverse mapping of manager classes to constants from the
Context class. |
static <MGR> MGR |
tryToGet(android.content.Context ctx,
java.lang.Class<MGR> cls)
As
get(android.content.Context, java.lang.Class<MGR>) , but executes the action inside a
try…catch structure, returning null if an
exception is thrown |
protected static final java.lang.String LOG_TAG
public static final java.lang.String INPUT_SERVICE
Context.INPUT_SERVICE
(added in Android API 16)public static final java.lang.String MEDIA_ROUTER_SERVICE
Context.MEDIA_ROUTER_SERVICE
(added in Android API 16)public static final java.lang.String NSD_SERVICE
Context.NSD_SERVICE
(added in Android API 16)public static final java.lang.String DISPLAY_SERVICE
Context.DISPLAY_SERVICE
(added in Android API 17)public static final java.lang.String USER_SERVICE
Context.USER_SERVICE
(added in Android API 17)public static final java.lang.String BLUETOOTH_SERVICE
Context.BLUETOOTH_SERVICE
(added in Android API 18)public static final java.lang.String APP_OPS_SERVICE
Context.APP_OPS_SERVICE
(added in Android API 19)public static final java.lang.String CAPTIONING_SERVICE
Context.CAPTIONING_SERVICE
(added in Android API 19)public static final java.lang.String CONSUMER_IR_SERVICE
Context.CONSUMER_IR_SERVICE
(added in Android API 19)public static final java.lang.String PRINT_SERVICE
Context.PRINT_SERVICE
(added in Android API 19)public static final java.lang.String APPWIDGET_SERVICE
Context.APPWIDGET_SERVICE
(added in Android API 21)public static final java.lang.String BATTERY_SERVICE
Context.BATTERY_SERVICE
(added in Android API 21)public static final java.lang.String CAMERA_SERVICE
Context.CAMERA_SERVICE
(added in Android API 21)public static final java.lang.String JOB_SCHEDULER_SERVICE
Context.JOB_SCHEDULER_SERVICE
(added in Android API 21)public static final java.lang.String LAUNCHER_APPS_SERVICE
Context.LAUNCHER_APPS_SERVICE
(added in Android API 21)public static final java.lang.String MEDIA_PROJECTION_SERVICE
Context.MEDIA_PROJECTION_SERVICE
(added in Android API 21)public static final java.lang.String MEDIA_SESSION_SERVICE
Context.MEDIA_SESSION_SERVICE
(added in Android API 21)public static final java.lang.String RESTRICTIONS_SERVICE
Context.RESTRICTIONS_SERVICE
(added in Android API 21)public static final java.lang.String TELECOM_SERVICE
Context.TELECOM_SERVICE
(added in Android API 21)public static final java.lang.String TV_INPUT_SERVICE
Context.TV_INPUT_SERVICE
(added in Android API 21)public static final java.lang.String TELEPHONY_SUBSCRIPTION_SERVICE
Context.TELECOM_SUBSCRIPTION_SERVICE
(added in Android API 22)public static final java.lang.String USAGE_STATS_SERVICE
Context.USAGE_STATS_SERVICE
(added in Android API 22)public static java.util.HashMap<java.lang.Class<?>,java.lang.String> REVERSE_MAP
Context
. Where those constants are
not defined until later API versions, the map instead uses one of the
compatibility constants defined in this class.
Context
were observed during unit testing.
Context Token | Documented Class | Actual Class | Resolution |
---|---|---|---|
Context.SENSOR_SERVICE |
SensorManager |
android.hardware.SystemSensorManager |
The actual class is assignable. |
Context.WALLPAPER_SERVICE |
android.service.wallpaper.WallpaperService |
WallpaperManager |
The returned class is not assignable; the map uses
WallpaperManager instead of
WallpaperService .
|
get(android.content.Context, java.lang.Class<MGR>)
method will simply call the
existing getSystemService(Class)
method in the API 23+ version of
Context
, so the map would not need to be populated.Context
protected static void initReverseMap(int nVersion)
Context
class. Invoked by a static initializer for the class
itself; the method is not intended to be called directly, except in unit
testing scenarios.nVersion
- (since 0.2.1/#53) the environment's current SDK
version; passed in as a parameter solely to support testing. See how
this is used "normally" in the static initializer.public static <MGR> MGR get(android.content.Context ctx, java.lang.Class<MGR> cls)
getSystemService(Class)
method instead of
executing its own compatibility logic.
The return value of this method is already an instance of the type
specified in the parameter; there is no need for additional typecasting
after the instance is returned to the caller. This is exactly how the
getSystemService(Class)
method works in the API 23+ version of
Context
.MGR
- the class to be obtained, as a generic return typectx
- the context in which to obtain the servicecls
- the class to be obtained, as a parameterpublic static <MGR> MGR tryToGet(android.content.Context ctx, java.lang.Class<MGR> cls)
get(android.content.Context, java.lang.Class<MGR>)
, but executes the action inside a
try…catch
structure, returning null
if an
exception is thrownMGR
- the class to be obtained, as a generic return typectx
- the context in which to obtain the servicecls
- the class to be obtained, as a parameter