gpt4 book ai didi

android - Android中的实用类、AsyncTask、松耦合请指教

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:41 24 4
gpt4 key购买 nike

我试图搜索有关此主题的任何讨论,但到目前为止我还没有找到任何有用的内容。因此,我决定继续发布这个。

所以我的查询是关于 Android 最佳实践的。我正在创建一个简单的应用程序,它调用 RESTful 端点,解析下载的 JSON,并在 Activity 中包含的一些 fragment 中显示结果。

我有一个扩展 AsyncTask 的自定义“实用程序”类。在 doInBackground 方法中,我发出请求,将响应存储在字符串中,等等(非常简单的东西)。

现在,我了解到 AsyncTask 中还有另外两种方法 - onPreExecute 和 onPostExecute。如果我在网上研究的是正确的,这些方法是我应该能够与 UI 交互的地方,方法是将我的上下文传递给这个实用程序类,通过 id 查找 View ,设置字段文本,或者我想要的任何东西做。

这就是我的问题。围绕这个的最佳实践是什么?

目前,我扩展 AsyncTask 并进行 Web 服务调用的实用程序类有一个私有(private)成员变量:

    private FragmentActivity mActivityContext;

然后,为了与 UI 交互,我执行如下操作:

@Override
protected void onPreExecute()
{
super.onPreExecute();
android.support.v4.app.FragmentManager fm = mActivityContext.getSupportFragmentManager();
MainActivity.PlaceholderFragment fragment = (MainActivity.PlaceholderFragment)fm.findFragmentByTag("PlaceholderFragment");

if (fragment != null)
{
TextView textView = (TextView)fragment.getView().findViewById(R.id.my_custom_field);
textView.setText("This field was updated through onPreExecute!");
}
}

虽然到目前为止这似乎工作正常,但我担心的是 - 实用程序类是否应该访问它引用的 Activity 的 fragment 管理器?它甚至应该引用该 Activity 吗?通过使用 fragment 管理器,实用程序类必须知道 fragment 标签。这不是一种非常模块化或松散耦合的方法。解决方案是否像在创建实用程序类的实例时传入 fragment 标记一样简单,而不是像现在这样硬设置?或者有更好的替代路线吗?

最佳答案

这就是我所做的,我从 StackOverflow 了解到的:

假设我们正在从服务器数据库下载类别

首先,创建一个接口(interface)

public interface OnCategoriesDownloadedListener {
public void onCategoriesDownloadSuccess(ArrayList<String> categories);
public void onCategoriesDownloadFailure(String reason);
}

然后,在等待下载这些类别的 Activity 中,我实现了这个接口(interface):

public class MyActivity extends Activity implements  OnCategoriesDownloadedListener {

@Override
onCategoriesDownloadSuccess(ArrayList<String> categories) {
//get the categories and do whatever my soul desire
}

@Override
onCategoriesDownloadFailure(String reason) {
//get the reason and come up with appropriate failure excuse
}

}

在我的异步任务类中,我执行以下操作

public class GetCategoriesFromServerAsync extends AsyncTask<String, String, ArrayList<String>> {


private OnCategoriesDownloadedListener listener;
private ArrayList<String> categoryNamesArray;
private String reason;

public GetCategoriesFromServerAsync(OnCategoriesDownloadedListener listener) {
this.listener = listener;
//usually I pass more data here, for example when Im registering a user in the server database, Im using the constructor to pass their data.
}

@Override
protected ArrayList<String> doInBackground(String... args) {
int success;

try {
//JSON stuff

return categoryNames;

} else if (success == 2) {
reason = "failure";
return null;
} else {
reason = "Connection error";
return null;
}
} catch (JSONException e) {
e.printStackTrace();
reason = "Connection error";
return null;
}
}

@Override
protected void onPostExecute(Object[] categoryNamesAndLogos) {

if (listener != null) {
if (categoryNames!=null) {
listener.onCategoriesDownloadSuccess(categoryNames);
} else if (reason.contentEquals(TAG_FAILURE)) {
listener.onCategoriesDownloadFailure(reason);
} else if (reason.contentEquals(TAG_CONNECTION_ERROR)) {
listener.onCategoriesDownloadFailure(reason);
}

}
super.onPostExecute(categoryNames);
}

从实现监听器的 Activity 中,我使用其构造函数启动异步任务:

new GetCategoriesFromDatabase(this).execute();

因此注意到该 Activity 正在实现监听器并正在等待结果。

在 asynctask postExecute 中我只处理结果,将它们发送到正在等待它们的 Activity 并在 onCategoriesDownloadSuccess 方法中处理它们

如果您想要显示一个加载程序,您可以同时从您的 Activity 中启动异步任务并在 onSuccess 和 onFailure 方法中结束显示它。此时,我在 AsyncTask 的 onPreExecute 和 onPostExecute 中执行此操作,但我需要花一些时间或寻求建议,并考虑哪种方法更好。

不确定这是否是最佳实践,因为这是我唯一知道的,但它确实对我有用。

关于android - Android中的实用类、AsyncTask、松耦合请指教,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20790390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com