在不同的class调用方法时如何获取Context?

How to get Context when you call a method in a different class?

基本上,我有一个方法需要 Context 才能执行多个任务。

public void openDatePicker() { Context c; }

我想知道如何定义方法调用者的上下文,这样我就不必在每次调用方法时都将其作为参数插入。

您可以使用众多依赖项注入库之一,不仅注入上下文,还注入特定文件中的任何依赖项。

你可以看看Dagger2例如

This 将帮助您准确创建不同的上下文

您可以在构造函数中定义一次,这样每次您要使用该方法时,都不必在参数中定义它。试试这个。

public class CustomDatePicker {

    private Context context;

    public ClassLocationModel(Context context) {
         this.context = context;
    }

    public void openDatePicker() {
         //Use context that has been passed through constructor here
    }
}

所以,基本上诀窍是只在构造函数的参数中传递上下文 ONCE 并且您可以重复使用 openDatePicker 方法而无需在参数中传递任何上下文。

这可以通过两种方式完成..

第一个

创建一个 class 说 Functions 并在其中创建 public 静态方法。

public class Functions{

    public static void openDatePicker(Context c){ 
        // Do your work where you can use c as Context
    }

}

随心所欲地称呼它

Function.openDatePicker(MainActivity.this);

第二

如@UmarZaii 建议您可以在构造函数中定义一次。

public class CustomDatePicker {

    private Context context;

    public ClassLocationModel(Context context) {
         this.context = context;
    }

    public void openDatePicker() {
         // Do your work where you can use "context" as Context
    }
} 

而且你必须在 Activity

中初始化此 class 一次
CustomDatePicker customDatePicker = new CustomDatePicker(MainActivity.this);

// to use its methods
customDatePicker.openDatePicker(); 

希望对您有所帮助。

让我们对 Context.

有一个基本的了解

Context 允许访问特定于应用程序的资源和 classes,以及对应用程序级操作的调用例如启动活动广播接收意图

一个上下文代表您的环境。它代表您在系统中所在位置的周围状态。

一个 Android 应用有 活动 Context 就像您的应用程序当前 运行 所在环境的句柄。 activity 对象继承了 上下文对象。

那么,如何在您的应用程序中访问 Context

  • getContext(): Returns 当前活动的上下文 Activity。您还可以使用 this 来展开布局和菜单、注册上下文菜单、实例化小部件、启动其他 activities, 在 Activity
  • 中创建新的 Intent

示例:View.getContext() returns the Context该视图当前位于 运行 中。通常当前处于活动状态 Activity.

View mView = this.getLayoutInflater().inflate(R.layout.myLayout, myViewGroup);

或者,

Intent mIntent = new Intent(this, MyActivity.class);
this.startActivity(mIntent);
  • getApplicationContext(): Returns 整个 applicationContext ,而不是当前的 Activity 即,如果您需要一个 Context 绑定到整个 application[ 的生命周期=135=],不仅仅是现在的Activity。这个 Context 被广泛使用,因为它是最安全的,因为这个 Contextapplication[=135= 的生命周期内存在].

示例:绑定应用程序范围 class.

Intent mIntent = new Intent(this, MyPersistent.class);
MyServiceConnection mServiceConnection = new MyServiceConnection();
if (mServiceConnection != null) {
    getApplicationContext().bindService(mIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
  • getBaseContext(): Returns一个Context来自另一个context您可以访问的应用程序。当你有一个 ContextWrapper 时,这个方法有一个限制 usage.The 方法是相关的。

ContextWrapper 是,"Proxying implementation of Context that simply delegates all of its calls to another Context. Can be subclassed to modify behavior without changing the original Context."(根据 javadocs)..

现在,可能会出现一个问题!如何访问不扩展 Activity[ 的 class 内的 Context 对象=135=] ?因此,可以通过以下方式实现:

  • 上下文作为对象传递给其他class:对于class 无法访问上述任何 Context上下文可以通过其构造函数.
  • 传递给这样的class

示例:

public class MyActivity extends Activity {
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        context = getApplicationContext();
        new Helper(context);
    }
}

public class Helper {
    Context mContext;
    Helper(Context ctx){
        this.mContext= ctx;
    }
//Now you can use this mContext anywhere in your class.
}

最后,要在 应用程序 的任何位置获得 Context,您可以定义一个新的 class 在您的 申请中 :

public class MyContext extends Application {
    private static MyContext instance;

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
    }

    public static Context getContext(){
        return instance;
        // or return instance.getApplicationContext();
    }
}

在您的 清单 中,您需要将此 class 添加到“名称”字段中的“Application”选项卡,如下所示:

<application
android:name="com.example.app.MyContext "
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
.......
<activity
......
</activity>

要在您的应用程序中的任何位置获取 Context,即在您的 项目的任何 class,你可以调用这个class函数,像这样:

MyContext.getContext();

这个函数将return一个上下文,你可以用它来执行你的任务。

所以,来回答你的问题。我想最后两种方法可以解决您的问题,即将 Context 对象传递给 class 并定义一个新的 class 扩展 Application.

这就是@Achmad Naufal Syafiq 的全部内容。这是我最大的努力来帮助你。