Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
public class

IsolatedContext

extends ContextWrapper
java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.test.IsolatedContext

Class Overview

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

[Expand]
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.
[Expand]
Inherited Methods
From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object

Public Constructors

public IsolatedContext ( ContentResolver resolver, Context targetContext)

Added in API level 1

Public Methods

public boolean bindService ( Intent service, ServiceConnection conn, int flags)

Added in API level 1

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 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 Receives information as the service is started and stopped. This must be a valid ServiceConnection object; it must not be null.
flags 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
  • 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.

public int checkUriPermission ( Uri uri, int pid, int uid, int modeFlags)

Added in API level 1

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 The uri that is being checked.
pid The process ID being checked against. Must be > 0.
uid The user ID being checked against. A uid of 0 is the root user, which will pass every permission check.
modeFlags 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

public int checkUriPermission ( Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags)

Added in API level 1

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 The Uri whose permission is to be checked, or null to not do this check.
readPermission The permission that provides overall read access, or null to not do this check.
writePermission The permission that provides overall write acess, or null to not do this check.
pid The process ID being checked against. Must be > 0.
uid The user ID being checked against. A uid of 0 is the root user, which will pass every permission check.
modeFlags 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

public List < Intent > getAndClearBroadcastIntents ()

Added in API level 1

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

public ContentResolver getContentResolver ()

Added in API level 1

Return a ContentResolver instance for your application's package.

public File getFilesDir ()

Added in API level 1

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

No permissions are required to read or write to the returned path, since this path is internal storage.

Returns
  • The path of the directory holding application files.

public Object getSystemService ( String name)

Added in API level 1

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

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 The name of the desired service.
Returns
  • The service or null if the name does not exist.

public Intent registerReceiver ( BroadcastReceiver receiver, IntentFilter filter)

Added in API level 1

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 The BroadcastReceiver to handle the broadcast.
filter Selects the Intent broadcasts to be received.
Returns
  • The first sticky intent found that matches filter , or null if there are none.

public void sendBroadcast ( Intent intent)

Added in API level 1

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 The Intent to broadcast; all receivers matching this Intent will receive the broadcast.

public void sendOrderedBroadcast ( Intent intent, String receiverPermission)

Added in API level 1

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 The Intent to broadcast; all receivers matching this Intent will receive the broadcast.
receiverPermission (optional) String naming a permissions that a receiver must hold in order to receive your broadcast. If null, no permission is required.

public void unregisterReceiver ( BroadcastReceiver receiver)

Added in API level 1

Unregister a previously registered BroadcastReceiver. All filters that have been registered for this BroadcastReceiver will be removed.

Parameters
receiver The BroadcastReceiver to unregister.