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 vm 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 setVmPolicy(final Class<?> strictMode) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> vmPolicyType = Class.forName("android.os.StrictMode$VmPolicy"); Method setVmPolicy = strictMode.getMethod("setVmPolicy", vmPolicyType); Class<?> vmPolicyBuilder = Class.forName("android.os.StrictMode$VmPolicy$Builder"); Object policy = buildPolicy(vmPolicyBuilder); setVmPolicy.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); } }