Java tutorial
//package com.java2s; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { /** * Sets the thread policy. * * @param strictMode * the strict mode to work with * @throws ClassNotFoundException * on problem * @throws NoSuchMethodException * on problem * @throws InstantiationException * on problem * @throws IllegalAccessException * on problem * @throws InvocationTargetException * on problem */ private static void setThreadPolicy(final Class<?> strictMode) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> threadPolicyType = Class.forName("android.os.StrictMode$ThreadPolicy"); Method setThreadPolicy = strictMode.getMethod("setThreadPolicy", threadPolicyType); Class<?> threadPolicyBuilder = Class.forName("android.os.StrictMode$ThreadPolicy$Builder"); Object policy = buildPolicy(threadPolicyBuilder); setThreadPolicy.invoke(strictMode, policy); } /** * Builds a policy with detectAll, penaltyLog and penaltyDeath. * * @param policyBuilder * the builder to use * @return the policy object * @throws NoSuchMethodException * on problem * @throws InstantiationException * on problem * @throws IllegalAccessException * on problem * @throws InvocationTargetException * on problem */ private static Object buildPolicy(final Class<?> policyBuilder) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Method detectAll = policyBuilder.getMethod("detectAll"); Method penaltyLog = policyBuilder.getMethod("penaltyLog"); Method penaltyDeath = policyBuilder.getMethod("penaltyDeath"); Method build = policyBuilder.getMethod("build"); Constructor<?> constructor = policyBuilder.getConstructor(); Object builder = constructor.newInstance(); builder = detectAll.invoke(builder); builder = penaltyLog.invoke(builder); builder = penaltyDeath.invoke(builder); return build.invoke(builder); } }