简体   繁体   English

我怎样才能在自己的类中创建一个私有方法来反复使用

[英]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 我试图用一个名为相同的构造函数创建一个名为isCallable的java类,但是我会迷失在构造函数中的内容,感谢任何帮助

 private boolean isCallable(Intent intent) {

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

}

Step #1: Create a Java class named HarropUtils . 步骤#1:创建一个名为HarropUtils的Java类。

Step #2: Implement the following static method on HarropUtils 步骤2:在HarropUtils上实现以下static方法

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 (eg, an Activity ) and the Intent to test. 步骤#3:从你需要的地方调用HarropUtils.isCallble() ,传入一个Context (例如,一个Activity )和Intent来测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中停止方法并重新开始 - How can i make a method stop and start over again in Java 一个类可以有自己的专用PrintWriter吗? - Can a class have its own private PrintWriter? 假设不涉及 class 变量,在 Java 中使用私有 static 方法有什么优势 - Assuming no class variables are involved, what are the advantages of using a private static method over a private method in Java 如何使位于自己文件中的私有节点 class 对我的卡组 class(LINKEDLIST)可见? - How to make a private node class located in its own file visible to my deck class (LINKEDLIST)? 如何将main方法分成自己的类? - How do I seperate main method into its own class? 使用 generics 使 class 的方法能够返回它自己和它的子类实例 - Make method of class using generics be able to return both its own and its subclasses instances 如何在libgdx中的x轴上旋转图像? - How can i rotate an image over its x axes in libgdx? 如何使用GWT和Sencha GXT像GXT Scheduler一样创建自己的Scheduler类? - How can I make my own Scheduler class just like GXT scheduler using GWT and Sencha GXT? 如何使toString()方法返回Super Class私有字段及其实例字段? - How make toString() method return Super Class private fields also along with its instance fields? 一个类如何访问自己的类名? - How can a class access its own classname?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM