Most visited

Recently visited

Added in API level 1
Deprecated since API level 24

IsolatedContext

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.

Summary

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 openFileOutput(String, int) are stored.

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

Public constructors

IsolatedContext

Added in API level 1
IsolatedContext (ContentResolver resolver, 
                Context targetContext)

Parameters
resolver ContentResolver
targetContext Context

Public methods

bindService

Added in API level 1
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.

checkUriPermission

Added in API level 1
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.

checkUriPermission

Added in API level 1
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.

getAndClearBroadcastIntents

Added in API level 1
List<Intent> getAndClearBroadcastIntents ()

Returns the list of intents that were broadcast since the last call to this method.

Returns
List<Intent>

getContentResolver

Added in API level 1
ContentResolver getContentResolver ()

Return a ContentResolver instance for your application's package.

Returns
ContentResolver

getFilesDir

Added in API level 1
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.

getSystemService

Added in API level 1
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")
The top-level window manager in which you can place custom windows. The returned object is a WindowManager.
LAYOUT_INFLATER_SERVICE ("layout_inflater")
A LayoutInflater for inflating layout resources in this context.
ACTIVITY_SERVICE ("activity")
A ActivityManager for interacting with the global activity state of the system.
POWER_SERVICE ("power")
A PowerManager for controlling power management.
ALARM_SERVICE ("alarm")
A AlarmManager for receiving intents at the time of your choosing.
NOTIFICATION_SERVICE ("notification")
A NotificationManager for informing the user of background events.
KEYGUARD_SERVICE ("keyguard")
A KeyguardManager for controlling keyguard.
LOCATION_SERVICE ("location")
A LocationManager for controlling location (e.g., GPS) updates.
SEARCH_SERVICE ("search")
A SearchManager for handling search.
VIBRATOR_SERVICE ("vibrator")
A Vibrator for interacting with the vibrator hardware.
CONNECTIVITY_SERVICE ("connection")
A ConnectivityManager for handling management of network connections.
WIFI_SERVICE ("wifi")
A WifiManager for management of Wi-Fi connectivity.
WIFI_P2P_SERVICE ("wifip2p")
A WifiP2pManager for management of Wi-Fi Direct connectivity.
INPUT_METHOD_SERVICE ("input_method")
An InputMethodManager for management of input methods.
UI_MODE_SERVICE ("uimode")
An UiModeManager for controlling UI modes.
DOWNLOAD_SERVICE ("download")
A DownloadManager for requesting HTTP downloads
BATTERY_SERVICE ("batterymanager")
A BatteryManager for managing battery state
JOB_SCHEDULER_SERVICE ("taskmanager")
A JobScheduler for managing scheduled tasks
NETWORK_STATS_SERVICE ("netstats")
A NetworkStatsManager for querying network usage statistics.
HARDWARE_PROPERTIES_SERVICE ("hardware_properties")
A 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.

registerReceiver

Added in API level 1
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.

sendBroadcast

Added in API level 1
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.

sendOrderedBroadcast

Added in API level 1
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.

unregisterReceiver

Added in API level 1
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.

Hooray!