public
class
IsolatedContext
extends ContextWrapper
java.lang.Object | |||
↳ | android.content.Context | ||
↳ | android.content.ContextWrapper | ||
↳ | android.test.IsolatedContext |
This class was deprecated
in API level 24.
New tests should be written using the
Android Testing Support Library.
A mock context which prevents its users from talking to the rest of the device while stubbing enough methods to satify code that tries to talk to other packages.
Inherited constants |
---|
From
class
android.content.Context
|
Public constructors | |
---|---|
IsolatedContext(ContentResolver resolver, Context targetContext)
|
Public methods | |
---|---|
boolean
|
bindService(Intent service, ServiceConnection conn, int flags)
Connect to an application service, creating it if needed. |
int
|
checkUriPermission(Uri uri, int pid, int uid, int modeFlags)
Determine whether a particular process and user ID has been granted permission to access a specific URI. |
int
|
checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)
Check both a Uri and normal permission. |
List<Intent>
|
getAndClearBroadcastIntents()
Returns the list of intents that were broadcast since the last call to this method. |
ContentResolver
|
getContentResolver()
Return a ContentResolver instance for your application's package. |
File
|
getFilesDir()
Returns the absolute path to the directory on the filesystem where files
created with |
Object
|
getSystemService(String name)
Return the handle to a system-level service by name. |
Intent
|
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
Register a BroadcastReceiver to be run in the main activity thread. |
void
|
sendBroadcast(Intent intent)
Broadcast the given intent to all interested BroadcastReceivers. |
void
|
sendOrderedBroadcast(Intent intent, String receiverPermission)
Broadcast the given intent to all interested BroadcastReceivers, delivering them one at a time to allow more preferred receivers to consume the broadcast before it is delivered to less preferred receivers. |
void
|
unregisterReceiver(BroadcastReceiver receiver)
Unregister a previously registered BroadcastReceiver. |
Inherited methods | |
---|---|
From
class
android.content.ContextWrapper
| |
From
class
android.content.Context
| |
From
class
java.lang.Object
|
IsolatedContext (ContentResolver resolver, Context targetContext)
Parameters | |
---|---|
resolver |
ContentResolver
|
targetContext |
Context
|
boolean bindService (Intent service, ServiceConnection conn, int flags)
Connect to an application service, creating it if needed. This defines a dependency between your application and the service. The given conn will receive the service object when it is created and be told if it dies and restarts. The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.
This function will throw SecurityException
if you do not
have permission to bind to the given service.
Note: this method can not be called from a
BroadcastReceiver
component. A pattern you can use to
communicate from a BroadcastReceiver to a Service is to call
startService(Intent)
with the arguments containing the command to be
sent, with the service calling its
stopSelf(int)
method when done executing
that command. See the API demo App/Service/Service Start Arguments
Controller for an illustration of this. It is okay, however, to use
this method from a BroadcastReceiver that has been registered with
registerReceiver(BroadcastReceiver, IntentFilter)
, since the lifetime of this BroadcastReceiver
is tied to another object (the one that registered it).
Parameters | |
---|---|
service |
Intent :
Identifies the service to connect to. The Intent may
specify either an explicit component name, or a logical
description (action, category, etc) to match an
IntentFilter published by a service. |
conn |
ServiceConnection :
Receives information as the service is started and stopped.
This must be a valid ServiceConnection object; it must not be null. |
flags |
int :
Operation options for the binding. May be 0,
BIND_AUTO_CREATE , BIND_DEBUG_UNBIND ,
BIND_NOT_FOREGROUND , BIND_ABOVE_CLIENT ,
BIND_ALLOW_OOM_MANAGEMENT , or
BIND_WAIVE_PRIORITY . |
Returns | |
---|---|
boolean |
If you have successfully bound to the service, true is returned;
false is returned if the connection is not made so you will not
receive the service object. |
int checkUriPermission (Uri uri, int pid, int uid, int modeFlags)
Determine whether a particular process and user ID has been granted permission to access a specific URI. This only checks for permissions that have been explicitly granted -- if the given process/uid has more general access to the URI's content provider then this check will always fail.
Parameters | |
---|---|
uri |
Uri :
The uri that is being checked. |
pid |
int :
The process ID being checked against. Must be > 0. |
uid |
int :
The user ID being checked against. A uid of 0 is the root
user, which will pass every permission check. |
modeFlags |
int :
The type of access to grant. May be one or both of
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION . |
Returns | |
---|---|
int |
PERMISSION_GRANTED if the given
pid/uid is allowed to access that uri, or
PERMISSION_DENIED if it is not. |
int checkUriPermission (Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)
Check both a Uri and normal permission. This allows you to perform
both checkPermission(String, int, int)
and checkUriPermission(Uri, int, int, int)
in one
call.
Parameters | |
---|---|
uri |
Uri :
The Uri whose permission is to be checked, or null to not
do this check. |
readPermission |
String :
The permission that provides overall read access,
or null to not do this check. |
writePermission |
String :
The permission that provides overall write
access, or null to not do this check. |
pid |
int :
The process ID being checked against. Must be > 0. |
uid |
int :
The user ID being checked against. A uid of 0 is the root
user, which will pass every permission check. |
modeFlags |
int :
The type of access to grant. May be one or both of
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION . |
Returns | |
---|---|
int |
PERMISSION_GRANTED if the caller
is allowed to access that uri or holds one of the given permissions, or
PERMISSION_DENIED if it is not.
|
List<Intent> getAndClearBroadcastIntents ()
Returns the list of intents that were broadcast since the last call to this method.
Returns | |
---|---|
List<Intent> |
ContentResolver getContentResolver ()
Return a ContentResolver instance for your application's package.
Returns | |
---|---|
ContentResolver |
File getFilesDir ()
Returns the absolute path to the directory on the filesystem where files
created with openFileOutput(String, int)
are stored.
The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.
No additional permissions are required for the calling app to read or write files under the returned path.
Returns | |
---|---|
File |
The path of the directory holding application files. |
Object getSystemService (String name)
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:
WINDOW_SERVICE
("window")
WindowManager
.
LAYOUT_INFLATER_SERVICE
("layout_inflater")
LayoutInflater
for inflating layout resources
in this context.
ACTIVITY_SERVICE
("activity")
ActivityManager
for interacting with the
global activity state of the system.
POWER_SERVICE
("power")
PowerManager
for controlling power
management.
ALARM_SERVICE
("alarm")
AlarmManager
for receiving intents at the
time of your choosing.
NOTIFICATION_SERVICE
("notification")
NotificationManager
for informing the user
of background events.
KEYGUARD_SERVICE
("keyguard")
KeyguardManager
for controlling keyguard.
LOCATION_SERVICE
("location")
LocationManager
for controlling location
(e.g., GPS) updates.
SEARCH_SERVICE
("search")
SearchManager
for handling search.
VIBRATOR_SERVICE
("vibrator")
Vibrator
for interacting with the vibrator
hardware.
CONNECTIVITY_SERVICE
("connection")
ConnectivityManager
for
handling management of network connections.
WIFI_SERVICE
("wifi")
WifiManager
for management of
Wi-Fi connectivity.
WIFI_P2P_SERVICE
("wifip2p")
WifiP2pManager
for management of
Wi-Fi Direct connectivity.
INPUT_METHOD_SERVICE
("input_method")
InputMethodManager
for management of input methods.
UI_MODE_SERVICE
("uimode")
UiModeManager
for controlling UI modes.
DOWNLOAD_SERVICE
("download")
DownloadManager
for requesting HTTP downloads
BATTERY_SERVICE
("batterymanager")
BatteryManager
for managing battery state
JOB_SCHEDULER_SERVICE
("taskmanager")
JobScheduler
for managing scheduled tasks
NETWORK_STATS_SERVICE
("netstats")
NetworkStatsManager
for querying network
usage statistics.
HARDWARE_PROPERTIES_SERVICE
("hardware_properties")
HardwarePropertiesManager
for accessing hardware properties.
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.)
Parameters | |
---|---|
name |
String :
The name of the desired service. |
Returns | |
---|---|
Object |
The service or null if the name does not exist. |
Intent registerReceiver (BroadcastReceiver receiver, IntentFilter filter)
Register a BroadcastReceiver to be run in the main activity thread. The receiver will be called with any broadcast Intent that matches filter, in the main application thread.
The system may broadcast Intents that are "sticky" -- these stay around after the broadcast as finished, to be sent to any later registrations. If your IntentFilter matches one of these sticky Intents, that Intent will be returned by this function and sent to your receiver as if it had just been broadcast.
There may be multiple sticky Intents that match filter, in which case each of these will be sent to receiver. In this case, only one of these can be returned directly by the function; which of these that is returned is arbitrarily decided by the system.
If you know the Intent your are registering for is sticky, you can supply null for your receiver. In this case, no receiver is registered -- the function simply returns the sticky Intent that matches filter. In the case of multiple matches, the same rules as described above apply.
See BroadcastReceiver
for more information on Intent broadcasts.
As of ICE_CREAM_SANDWICH
, receivers
registered with this method will correctly respect the
setPackage(String)
specified for an Intent being broadcast.
Prior to that, it would be ignored and delivered to all matching registered
receivers. Be careful if using this for security.
Note: this method cannot be called from a
BroadcastReceiver
component; that is, from a BroadcastReceiver
that is declared in an application's manifest. It is okay, however, to call
this method from another BroadcastReceiver that has itself been registered
at run time with registerReceiver(BroadcastReceiver, IntentFilter)
, since the lifetime of such a
registered BroadcastReceiver is tied to the object that registered it.
Parameters | |
---|---|
receiver |
BroadcastReceiver :
The BroadcastReceiver to handle the broadcast. |
filter |
IntentFilter :
Selects the Intent broadcasts to be received. |
Returns | |
---|---|
Intent |
The first sticky intent found that matches filter, or null if there are none. |
void sendBroadcast (Intent intent)
Broadcast the given intent to all interested BroadcastReceivers. This
call is asynchronous; it returns immediately, and you will continue
executing while the receivers are run. No results are propagated from
receivers and receivers can not abort the broadcast. If you want
to allow receivers to propagate results or abort the broadcast, you must
send an ordered broadcast using
sendOrderedBroadcast(Intent, String)
.
See BroadcastReceiver
for more information on Intent broadcasts.
Parameters | |
---|---|
intent |
Intent :
The Intent to broadcast; all receivers matching this
Intent will receive the broadcast. |
void sendOrderedBroadcast (Intent intent, String receiverPermission)
Broadcast the given intent to all interested BroadcastReceivers, delivering them one at a time to allow more preferred receivers to consume the broadcast before it is delivered to less preferred receivers. This call is asynchronous; it returns immediately, and you will continue executing while the receivers are run.
See BroadcastReceiver
for more information on Intent broadcasts.
Parameters | |
---|---|
intent |
Intent :
The Intent to broadcast; all receivers matching this
Intent will receive the broadcast. |
receiverPermission |
String :
(optional) String naming a permissions that
a receiver must hold in order to receive your broadcast.
If null, no permission is required. |
void unregisterReceiver (BroadcastReceiver receiver)
Unregister a previously registered BroadcastReceiver. All filters that have been registered for this BroadcastReceiver will be removed.
Parameters | |
---|---|
receiver |
BroadcastReceiver :
The BroadcastReceiver to unregister. |