我创建了一个示例AsyncTask 来向您展示如何处理与 ui 线程分开的数据。这个AsyncTask 将从资源中加载String 并将其设置为TextView。
重要提示:通常您永远不需要AsyncTask 来从资源中加载String。我只是选择这个作为示例来展示如何在单独的线程中执行工作。
对于任何AsyncTask,只需记住一件事:您在单独的线程中执行工作,而不是在 ui 线程中。当单独的线程完成时,无法保证您引用的 ui 元素仍然存在,甚至您的应用程序仍在运行。因此,如果您引用 Activity 或 Context 或某些 ui 元素,如 TextView 或 Button 或任何其他在后台线程完成时可能不再存在的复杂对象,您必须使用 WeakReference . WeakReferences 是解决此问题的一种巧妙方法,它允许对您引用的对象进行垃圾收集。然后,在您的AsyncTask 中,您可以检查您需要的对象是否仍然存在,如果是,则执行您的工作而不会造成内存泄漏。你像这样使用WeakReference:
WeakReference<TextView> textViewReference = new WeakReference<TextView>(textView);
如您所见,WeakReference 是一个泛型类型。如果您想在这种情况下引用TextView,您必须通过在WeakReference 之后写<TextView> 来告诉WeakReference。如果您要引用Context,则应写为WeakReference<Context>,或者如果您引用SQliteDatabase,则应写为WeakReference<SQLiteDatabase>。我猜你看到了这里的模式。
您将要引用的对象的实例传递给WeakReference 的构造函数。这就是上面示例的(textView) 部分。如果您以后想从WeakReference 取回实例,您可以这样做:
TextView textView = textViewReference.get();
如果您引用的 TextView 不再存在,get() 将简单地返回 null。因此,要检查 TextView 是否仍然存在,您可以简单地这样做:
TextView textView = textViewReference.get();
if(textView != null) {
// TextView still exists.
} else {
// TextView doesn't exist anymore. Most likely he has already been garbage collected.
}
如果您了解有关如何以及何时使用WeakReferences 的简单原则,那么实现AsyncTask 应该没有问题。我已经用 cmets 解释了有关 AsyncTask 的任何其他重要信息,只需查看源代码即可。如果您有任何其他问题,请随时提出。
public class ExampleTask extends AsyncTask<Void, Void, String> {
// We use WeakReferences for all complex objects to prevent memory leaks
private final WeakReference<TextView> textViewRefernece;
private final WeakReference<Context> contextReference;
// Primitives are fine. WeakReferences are not needed here.
private int resourceId;
public ExampleTask(Context context, TextView textView, int resourceId) {
// We create the WeakReferences to our Context and to the TextView in which we want the result to appear.
this.contextReference = new WeakReference<Context>(context);
this.textViewRefernece = new WeakReference<TextView>(textView);
this.resourceId = resourceId;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// This method is executed on the ui thread just before the background thread starts.
// You can perform some inital setup here but mostly you can leave this empty
}
@Override
protected String doInBackground(Void... params) {
// This method is executed in a background thread. Here we can do our work.
// First we have to get the context from the WeakReference
Context context = contextReference.get();
// Now we check if the Context still exists. If the context is null it has already been garbage collected.
// That would happen for example if the app has been closed or even if it crashed.
if(context != null) {
// The context is not null which means it still exists we can continue and load the required String and return it
return context.getString(this.resourceId);
}
// If something went wrong we return null.
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// This method is executed on the ui thread after the background thread is finished.
// Here we get our final result and can set it to our TextView
// First we check if the String result is null. If it is not null the background thread has finished successfully and we can continue.
if(result != null) {
// Here we get our TextView from the WeakReference
TextView textView = textViewRefernece.get();
// Now we check if the TextView still exists. If it is null it has already been garbage collected.
// That would happen for example if the app has been closed while the AsyncTask has been running.
// Or simply if the Fragment has been replaced or the Activity has changed.
if(textView != null) {
// The TextView is not null which means it still exists. We can now set our text.
textView.setText(result);
}
}
}
}