本文整理汇总了Java中android.os.StrictMode.ThreadPolicy类的典型用法代码示例。如果您正苦于以下问题:Java ThreadPolicy类的具体用法?Java ThreadPolicy怎么用?Java ThreadPolicy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ThreadPolicy类属于android.os.StrictMode包,在下文中一共展示了ThreadPolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newThread
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
@Override
public synchronized Thread newThread(@NonNull Runnable runnable) {
final Thread result = new Thread(runnable, "glide-" + name + "-thread-" + threadNum) {
@Override
public void run() {
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_BACKGROUND
+ android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE);
if (preventNetworkOperations) {
StrictMode.setThreadPolicy(
new ThreadPolicy.Builder()
.detectNetwork()
.penaltyDeath()
.build());
}
try {
super.run();
} catch (Throwable t) {
uncaughtThrowableStrategy.handle(t);
}
}
};
threadNum++;
return result;
}
示例2: getCoreCountPre17
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
/**
* Determines the number of cores available on the device (pre-v17).
*
* <p>Before Jellybean, {@link Runtime#availableProcessors()} returned the number of awake cores,
* which may not be the number of available cores depending on the device's current state. See
* https://stackoverflow.com/a/30150409.
*
* @return the maximum number of processors available to the VM; never smaller than one
*/
@SuppressWarnings("PMD")
private static int getCoreCountPre17() {
// We override the current ThreadPolicy to allow disk reads.
// This shouldn't actually do disk-IO and accesses a device file.
// See: https://github.com/bumptech/glide/issues/1170
File[] cpus = null;
ThreadPolicy originalPolicy = StrictMode.allowThreadDiskReads();
try {
File cpuInfo = new File(CPU_LOCATION);
final Pattern cpuNamePattern = Pattern.compile(CPU_NAME_REGEX);
cpus = cpuInfo.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return cpuNamePattern.matcher(s).matches();
}
});
} catch (Throwable t) {
if (Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, "Failed to calculate accurate cpu count", t);
}
} finally {
StrictMode.setThreadPolicy(originalPolicy);
}
return Math.max(1, cpus != null ? cpus.length : 0);
}
示例3: newThread
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
@Override
public synchronized Thread newThread(@NonNull Runnable runnable) {
final Thread result = new Thread(runnable, "glide-" + name + "-thread-" + threadNum) {
@Override
public void run() {
android.os.Process.setThreadPriority(DEFAULT_PRIORITY);
if (preventNetworkOperations) {
StrictMode.setThreadPolicy(
new ThreadPolicy.Builder()
.detectNetwork()
.penaltyDeath()
.build());
}
try {
super.run();
} catch (Throwable t) {
uncaughtThrowableStrategy.handle(t);
}
}
};
threadNum++;
return result;
}
示例4: initializeStrictMode
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
private static void initializeStrictMode() {
if (!strictModeInitialized) {
if (SDK_INT >= GINGERBREAD) {
ThreadPolicy.Builder threadPolicy = new ThreadPolicy.Builder();
VmPolicy.Builder vmPolicy = new VmPolicy.Builder();
threadPolicy.detectAll()
.penaltyLog();
vmPolicy.detectAll()
.penaltyLog();
if (STRICT_MODE_KILL_ON_ERROR) {
threadPolicy.penaltyDeath();
vmPolicy.penaltyDeath();
}
StrictMode.setThreadPolicy(threadPolicy.build());
StrictMode.setVmPolicy(vmPolicy.build());
strictModeInitialized = true;
}
}
}
示例5: zzb
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
public static <T> T zzb(Callable<T> paramCallable)
{
StrictMode.ThreadPolicy localThreadPolicy = StrictMode.getThreadPolicy();
try
{
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(localThreadPolicy).permitDiskReads().permitDiskWrites().build());
Object localObject2 = paramCallable.call();
return localObject2;
}
catch (Throwable localThrowable)
{
zzb.e("Unexpected exception.", localThrowable);
zzp.zzbL().zzb(localThrowable, true);
return null;
}
finally
{
StrictMode.setThreadPolicy(localThreadPolicy);
}
}
示例6: calculateBestThreadCount
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
/**
* Determines the number of cores available on the device.
*
* <p>{@link Runtime#availableProcessors()} returns the number of awake cores, which may not
* be the number of available cores depending on the device's current state. See
* http://goo.gl/8H670N.
*/
public static int calculateBestThreadCount() {
// We override the current ThreadPolicy to allow disk reads.
// This shouldn't actually do disk-IO and accesses a device file.
// See: https://github.com/bumptech/glide/issues/1170
ThreadPolicy originalPolicy = StrictMode.allowThreadDiskReads();
File[] cpus = null;
try {
File cpuInfo = new File(CPU_LOCATION);
final Pattern cpuNamePattern = Pattern.compile(CPU_NAME_REGEX);
cpus = cpuInfo.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return cpuNamePattern.matcher(s).matches();
}
});
} catch (Throwable t) {
if (Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, "Failed to calculate accurate cpu count", t);
}
} finally {
StrictMode.setThreadPolicy(originalPolicy);
}
int cpuCount = cpus != null ? cpus.length : 0;
int availableProcessors = Math.max(1, Runtime.getRuntime().availableProcessors());
return Math.min(MAXIMUM_AUTOMATIC_THREAD_COUNT, Math.max(availableProcessors, cpuCount));
}
示例7: enableStrictMode
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
private void enableStrictMode() {
if (TESTING) {
ThreadPolicy.Builder threadPolicy = new ThreadPolicy.Builder();
threadPolicy.detectAll();
threadPolicy.penaltyLog();
StrictMode.setThreadPolicy(threadPolicy.build());
VmPolicy.Builder vmPolicy = new VmPolicy.Builder();
vmPolicy.detectAll();
vmPolicy.penaltyLog();
StrictMode.setVmPolicy(vmPolicy.build());
}
}
示例8: resolveActivity
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
/**
* Retrieves the single activity that matches the given intent, or null if none found.
* @param intent The intent to query.
* @return The matching activity.
*/
public ResolveInfo resolveActivity(Intent intent) {
ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
return ContextUtils.getApplicationContext().getPackageManager().resolveActivity(
intent, 0);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
示例9: getActivitiesThatCanRespondToIntent
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
/**
* Retrieves the list of activities that can respond to the given intent.
* @param intent The intent to query.
* @return The list of activities that can respond to the intent.
*/
public List<ResolveInfo> getActivitiesThatCanRespondToIntent(Intent intent) {
ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
return ContextUtils.getApplicationContext().getPackageManager().queryIntentActivities(
intent, 0);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
示例10: getActivitiesThatCanRespondToIntentWithMetaData
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
/**
* Retrieves the list of activities that can respond to the given intent. And returns the
* activites' meta data in ResolveInfo.
*
* @param intent The intent to query.
* @return The list of activities that can respond to the intent.
*/
public List<ResolveInfo> getActivitiesThatCanRespondToIntentWithMetaData(Intent intent) {
ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
return ContextUtils.getApplicationContext().getPackageManager().queryIntentActivities(
intent, PackageManager.GET_META_DATA);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
示例11: getServicesThatCanRespondToIntent
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
/**
* Retrieves the list of services that can respond to the given intent.
* @param intent The intent to query.
* @return The list of services that can respond to the intent.
*/
public List<ResolveInfo> getServicesThatCanRespondToIntent(Intent intent) {
ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
return ContextUtils.getApplicationContext().getPackageManager().queryIntentServices(
intent, 0);
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
}
示例12: enableNasty
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static Object enableNasty() {
ThreadPolicy oldThreadPolicy = StrictMode.getThreadPolicy();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);
return oldThreadPolicy;
}
示例13: revertNasty
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static void revertNasty(@Nullable ThreadPolicy oldPolicy) {
if (oldPolicy == null) {
return;
}
StrictMode.setThreadPolicy(oldPolicy);
}
示例14: permit
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
/**
* @param policy
* @param runnable
* @return
*/
@TargetApi(VERSION_CODES.HONEYCOMB)
public void permit(final Policy.Thread policy, final Runnable runnable) {
final ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
final ThreadPolicy.Builder builder = new ThreadPolicy.Builder(oldPolicy);
switch (policy) {
case PermitAll: {
builder.permitAll();
break;
}
case PermitCustomSlowCalls: {
if (AndroidUtils.isAtLeastHoneycomb()) {
builder.permitCustomSlowCalls();
}
break;
}
case PermitDiskReads: {
builder.permitDiskReads();
break;
}
case PermitDiskWrites: {
builder.permitDiskWrites();
break;
}
case PermitNetwork: {
builder.permitNetwork();
break;
}
}
StrictMode.setThreadPolicy(builder.build());
if (runnable != null) {
runnable.run();
}
StrictMode.setThreadPolicy(oldPolicy);
}
示例15: reset
import android.os.StrictMode.ThreadPolicy; //导入依赖的package包/类
/**
* @return The current instance of {@link StrictModeHelper}.
*/
public StrictModeHelper reset() {
if (!AppHelper.with(context).isDebuggable()) {
return this;
}
threadBuilder = new ThreadPolicy.Builder();
vmBuilder = new VmPolicy.Builder();
return this;
}