android - 如何获取模块中没有应用程序类的非活动类的上下文
问题描述
我正在尝试获取在我的应用程序模块中的 sharedprefs 中使用的上下文。该模块没有扩展任何活动类,应用程序类也没有。下面是代码:
public class GetMovieCreditsUseCase extends BaseUseCase {
public static String director = "";
public static boolean loaded = false;
public interface GetMovieCreditsUseCaseCallback extends BaseUseCaseCallback {
void onImagesUrlsLoaded(List<ImageEntity> backdrops, List<ImageEntity> posters);
}
private String apiKey;
private int movieID;
public GetMovieCreditsUseCase(String apiKey, int movieID, GetMovieCreditsUseCaseCallback callback) {
super(callback);
this.movieID = movieID;
this.apiKey = apiKey;
}
@Override
public void onRun() throws Throwable {
API.http().movieDirectors(apiKey, movieID, new Callback<GetMovieCreditsResponse>() {
@Override
public void success(GetMovieCreditsResponse getMovieCreditsResponse, Response response) {
((GetMovieCreditsUseCaseCallback) callback).onImagesUrlsLoaded(getMovieCreditsResponse.getBackdrops(), getMovieCreditsResponse.getPosters());
String json = new Gson().toJson(getMovieCreditsResponse);
loaded = false;
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray c = null;
try {
c = jsonObj.getJSONArray("crew");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < c.length(); i++) {
JSONObject obj = null;
try {
obj = c.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
String A = null;
String B = null;
try {
A = obj.getString("name");
B = obj.getString("job");
} catch (JSONException e) {
e.printStackTrace();
}
if (B.equals("Director")) {
Log.d("Director", "Director is " + A);
director = A;
loaded = true;
//getSharedPreferences throws error as cannotResolveMethod
SharedPreferences prefs = getSharedPreferences("director", Context.MODE_PRIVATE);
prefs.registerOnSharedPreferenceChangeListener(
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(
SharedPreferences prefs, String key) {
System.out.println(key);
}
});
break;
}
System.out.println(A);
}
Log.d("Directing", json);
}
@Override
public void failure(RetrofitError error) {
if (error.getKind() == RetrofitError.Kind.NETWORK) {
errorReason = NETWORK_ERROR;
} else {
errorReason = error.getResponse().getReason();
}
onCancel();
}
});
}
}
我尝试了不同的解决方案,但无济于事。getApplicationContext、getActivity、this 或 MyApplication.getContext() 都不起作用(全部抛出错误)
解决方案
选项 1:通过构造函数传递上下文
public class GetMovieCreditsUseCase extends BaseUseCase {
private Context mContext;
public GetMovieCreditsUseCase(Context context, String apiKey, int movieID,GetMovieCreditsUseCaseCallback callback) {
super(callback);
this.mContext = context;
this.movieID = movieID;
this.apiKey = apiKey;
}
}
选项 2:定义您自己的应用程序类
AndroidManifest.xml
<application
android:name=".application.MyApplication"
....
我的应用程序.java
public class MyApplication extends Application {
private static MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static Context getAppContext() {
return mInstance.getApplicationContext();
}
}
然后调用 MyApplication.getAppContext()
推荐阅读
- wordpress - 调用未定义函数 request_currencies()
- python - AttributeError:“维度”对象在使用 Keras Sequential Model.fit 时没有属性“log10”
- c++11 - Pytorch 扩展:由 g++ 编译的扩展与由 setuptools 构建的扩展之间的性能差异
- python - 如何实现并行,以这样的方式延迟,当输出低于阈值时并行化 for 循环停止?
- javascript - 为什么使用 react-graph-vis 文本会重叠?
- python - 如何在OpenCV中绘制与圆弧垂直的线
- powershell - 使用 Powershell 将图像插入 Powerpoint
- r - R中的堆叠水平条形图
- python - Python:Selenium:无法获取浏览器控制台日志
- webstorm - 如何更改面包屑顶部边框?