下面列出了android.os.StrictMode#getThreadPolicy ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Slice onBindSliceStrict(Uri sliceUri, List<SliceSpec> supportedSpecs) {
ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
try {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyDeath()
.build());
return onBindSlice(sliceUri, new ArraySet<>(supportedSpecs));
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
/**
* Convenience method for disabling StrictMode for slow calls with try-with-resources.
*/
public static StrictModeContext allowSlowCalls() {
StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder(oldPolicy).permitCustomSlowCalls().build());
return new StrictModeContext(oldPolicy);
}
static public boolean runWithNetwork(Action action) {
StrictMode.ThreadPolicy currentPolicy = StrictMode.getThreadPolicy();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
try {
return action.run();
} finally {
StrictMode.setThreadPolicy(currentPolicy);
}
}
private void initFabricLax() {
StrictMode.ThreadPolicy old = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX);
try {
initFabric();
}
finally {
StrictMode.setThreadPolicy(old);
}
}
/**
* 检查端口是否可用
*
* @param port 端口
* @return 是否可用
*/
public static boolean isPortAvailable(int port) {
boolean available;
StrictMode.ThreadPolicy defaultPolicy = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
try {
bindPort("0.0.0.0", port);
bindPort(InetAddress.getLocalHost().getHostAddress(), port);
available = true;
} catch (IOException e) {
available = false;
}
StrictMode.setThreadPolicy(defaultPolicy);
return available;
}
public static void reloadPrefs()
{
if(Res.modPrefs == null)
return;
final StrictMode.ThreadPolicy tp = StrictMode.getThreadPolicy();
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX);
Res.modPrefs.reload();
StrictMode.setThreadPolicy(tp);
}
/**
* Sets a new StrictMode.ThreadPolicy based on the current one, but allows disk reads
* and disk writes.
*
* The return value is the old policy, which must be applied after the disk access is finished,
* by using StrictMode.setThreadPolicy(oldPolicy).
*
* @return the policy before allowing reads and writes.
*/
private static StrictMode.ThreadPolicy temporarilyAllowDiskWritesAndDiskReads() {
StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
StrictMode.ThreadPolicy.Builder newPolicy =
new StrictMode.ThreadPolicy.Builder(oldPolicy);
newPolicy.permitDiskReads();
newPolicy.permitDiskWrites();
StrictMode.setThreadPolicy(newPolicy.build());
return oldPolicy;
}
/**
* Sets a new StrictMode.ThreadPolicy based on the current one, but allows disk reads
* and disk writes.
*
* The return value is the old policy, which must be applied after the disk access is finished,
* by using StrictMode.setThreadPolicy(oldPolicy).
*
* @return the policy before allowing reads and writes.
*/
private static StrictMode.ThreadPolicy temporarilyAllowDiskWritesAndDiskReads() {
StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
StrictMode.ThreadPolicy.Builder newPolicy =
new StrictMode.ThreadPolicy.Builder(oldPolicy);
newPolicy.permitDiskReads();
newPolicy.permitDiskWrites();
StrictMode.setThreadPolicy(newPolicy.build());
return oldPolicy;
}
public void testEnableStrictModeForDevRelease() {
StrictModeUtils.enableStrictModeForDevRelease();
StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
//No way to test this, uses masks that we don't have access to.
//Is is to make sure no crashes occur.
}