Search code examples
javaandroidclassprivatepublic

how can i make a private method in to its own class for using over and over


i have this method which is in my main activity, but i want to use it in other classes, how can i write it into its own class so it can be called when ever? i have tried to make a java class called isCallable with a constructor called the same but im getting lost with what to put in the constructor thanks for any help

 private boolean isCallable(Intent intent) {

    if (intent == null) {
        return false;
    }
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;

}

Solution

  • Step #1: Create a Java class named HarropUtils.

    Step #2: Implement the following static method on HarropUtils

    static boolean isCallable(Context ctxt, Intent intent) {
    
        if (intent == null) {
            return false;
        }
        List<ResolveInfo> list = ctxt.getPackageManager().queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    
    }
    

    Step #3: Call HarropUtils.isCallble() from wherever you need it, passing in a Context (e.g., an Activity) and the Intent to test.