public class IsolatedContext
extends ContextWrapper
java.lang.Object | |||
↳ | android.content.Context | ||
↳ | android.content.ContextWrapper | ||
↳ | android.test.IsolatedContext |
此类在API级别24中已被弃用。
新的测试应该使用Android Testing Support Library编写 。
一种模拟上下文,它阻止其用户与设备的其余部分通话,同时存储足够的方法以使试图与其他软件包对话的代码齐全。
Inherited constants |
---|
From class android.content.Context
|
Public constructors |
|
---|---|
IsolatedContext(ContentResolver resolver, Context targetContext) |
Public methods |
|
---|---|
boolean |
bindService(Intent service, ServiceConnection conn, int flags) 连接到应用程序服务,根据需要创建它。 |
int |
checkUriPermission(Uri uri, int pid, int uid, int modeFlags) 确定是否已授予特定进程和用户标识访问特定URI的权限。 |
int |
checkUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags) 检查Uri和普通权限。 |
List<Intent> |
getAndClearBroadcastIntents() 返回自上次调用此方法以来广播的意向列表。 |
ContentResolver |
getContentResolver() 为你的应用程序包返回一个ContentResolver实例。 |
File |
getFilesDir() 返回存储使用 |
Object |
getSystemService(String name) 按名称将句柄返回到系统级服务。 |
Intent |
registerReceiver(BroadcastReceiver receiver, IntentFilter filter) 注册BroadcastReceiver以在主要活动线程中运行。 |
void |
sendBroadcast(Intent intent) 将给定的意图广播给所有感兴趣的BroadcastReceivers。 |
void |
sendOrderedBroadcast(Intent intent, String receiverPermission) 将给定的意图广播给所有感兴趣的BroadcastReceivers,一次发送一个,让更多的首选接收者在将广播发送给次优先接收者之前消费广播。 |
void |
unregisterReceiver(BroadcastReceiver receiver) 取消注册以前注册的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)
连接到应用程序服务,根据需要创建它。 这定义了应用程序和服务之间的依赖关系。 给定的conn在创建时会收到服务对象,并被告知它是否会死亡并重新启动。 只要存在调用上下文,该服务就会被系统认为是必需的。 例如,如果此Context是一个已停止的Activity,则该服务将不需要继续运行,直到活动恢复。
如果您没有绑定到给定服务的权限,则此函数将抛出 SecurityException
。
注意:此方法不能从BroadcastReceiver
组件调用 。 您可以用来从BroadcastReceiver与服务进行通信的模式是使用包含要发送的命令的参数调用startService(Intent)
,该服务在执行该命令时调用其stopSelf(int)
方法。 请参阅API演示应用程序/服务/服务启动参数控制器以获取此示例。 但是,从已经注册为registerReceiver(BroadcastReceiver, IntentFilter)
的BroadcastReceiver使用此方法是可以的,因为此BroadcastReceiver的生命周期与另一个对象(注册该对象的生命周期)相关联。
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)
确定是否已授予特定进程和用户标识访问特定URI的权限。 这只会检查已被显式授予的权限 - 如果给定的进程/ uid对URI的内容提供者具有更多的一般访问权限,则此检查将始终失败。
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)
检查Uri和普通权限。 这允许您在一个呼叫中执行checkPermission(String, int, int)
和checkUriPermission(Uri, int, int, int)
。
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 | |
---|---|
List<Intent> |
ContentResolver getContentResolver ()
为你的应用程序包返回一个ContentResolver实例。
Returns | |
---|---|
ContentResolver |
File getFilesDir ()
返回存储使用 openFileOutput(String, int)
创建的文件的文件系统上目录的绝对路径。
如果调用应用程序移动到采用的存储设备,则返回的路径可能会随时间而改变,因此只应保留相对路径。
调用应用程序无需额外的权限即可读取或写入返回路径下的文件。
Returns | |
---|---|
File |
The path of the directory holding application files. |
Object getSystemService (String name)
按名称将句柄返回到系统级服务。 返回的对象的类因所请求的名称而异。 目前可用的名称是:
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.
注意:通过此API获取的系统服务可能与它们从中获取的上下文紧密相关。 一般来说,不要在各种不同的上下文(活动,应用程序,服务,提供者等)之间共享服务对象,
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)
注册BroadcastReceiver以在主要活动线程中运行。 接收器将在主应用程序线程中与任何匹配过滤器的广播Intent一起调用。
系统可能会广播“粘性”的内容 - 在广播结束后,这些内容将保留下来,以便发送给任何以后的注册。 如果您的IntentFilter匹配这些粘意图之一,这一意图将这个函数返回时就好像刚刚被广播发送到您的接收器 。
可能有多个匹配过滤器的粘性内容,在这种情况下,每个内容都将被发送到接收方 。 在这种情况下,函数只能直接返回其中的一个; 哪些返回是由系统任意决定的。
如果您知道您注册的意图是粘性的,您可以为您的接收器提供空值。 在这种情况下,没有接收者注册 - 该函数只是返回匹配过滤器的粘性Intent。 在多个匹配的情况下,应用与上述相同的规则。
有关意向广播的更多信息,请参阅 BroadcastReceiver
。
从ICE_CREAM_SANDWICH
,使用此方法注册的接收方将正确地遵守为广播的Intent指定的setPackage(String)
。 在此之前,它将被忽略并交付给所有匹配的注册接收者。 如果使用它来保证安全,请小心。
注意:此方法不能从BroadcastReceiver
组件调用; 也就是从应用程序清单中声明的BroadcastReceiver。 然而,从另一个BroadcastReceiver调用此方法是可以的,因为这种已注册的BroadcastReceiver的生命周期与注册该对象的对象绑定在一起,而该方法本身已在运行时注册为registerReceiver(BroadcastReceiver, IntentFilter)
。
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)
将给定的意图广播给所有感兴趣的BroadcastReceivers。 这个调用是异步的; 它会立即返回,并且您将在接收器运行时继续执行。 没有结果从接收器传播,接收器不能中止广播。 如果您希望允许接收者传播结果或中止广播,则必须使用sendOrderedBroadcast(Intent, String)
发送有序广播。
有关意向广播的更多信息,请参阅 BroadcastReceiver
。
Parameters | |
---|---|
intent |
Intent : The Intent to broadcast; all receivers matching this Intent will receive the broadcast. |
void sendOrderedBroadcast (Intent intent, String receiverPermission)
将给定的意图广播给所有感兴趣的BroadcastReceivers,一次发送一个,让更多的首选接收者在将广播发送给次优先接收者之前消费广播。 这个调用是异步的; 它会立即返回,并且您将在接收器运行时继续执行。
有关意向广播的更多信息,请参阅 BroadcastReceiver
。
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)
取消注册以前注册的BroadcastReceiver。 所有为此BroadcastReceiver注册的过滤器都将被删除。
Parameters | |
---|---|
receiver |
BroadcastReceiver : The BroadcastReceiver to unregister. |