call Boolean Return Method - Android java.lang.reflect

Android examples for java.lang.reflect:Method Invoke

Description

call Boolean Return Method

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

import android.util.Log;

public class Main {
    public static boolean callBooleanReturnMethod(final Object instance,
            final String methodName, boolean showError) {
        return callBooleanReturnMethod(instance, methodName, null,
                showError);/*from  ww  w .j  ava2 s  . com*/
    }

    public static boolean callBooleanReturnMethod(final Object instance,
            final String methodName, final Class[] paramTypes,
            boolean showError, final Object... params) {
        try {
            final Method method = instance.getClass().getMethod(methodName,
                    paramTypes);
            final Boolean result = (Boolean) method
                    .invoke(instance, params);

            if (result == null || !result) {
                return false;
            }
        } catch (Exception e) {
            if (showError) {
                Log.e("SweetBlue", "Problem calling method: " + methodName
                        + " - " + e);
            }

            return false;
        }

        return true;
    }
}

Related Tutorials