在 Android 应用程序中,如果已知活动类名称,非活动类是否有任何方法可以获取上下文?
在 Android 应用程序中,如果已知活动类名称,非活动类是否有任何方法可以获取上下文?
class YourNonActivityClass{
// variable to hold context
private Context context;
//save the context recievied via constructor in a local variable
public YourNonActivityClass(Context context){
this.context=context;
}
}
您可以按照以下步骤从Activity中创建此类的实例:
new YourNonActivityClass(this);
Fragment
,请参考https://dev59.com/r2sy5IYBdhLWcg3w8Shc。 - dinosaur