[英]getApplicationContext in AsyncTask class?
I have a couple private AsyncTask methods that I'd like to break out into public external classes. 我有几个私有的AsyncTask方法,我想打破公共外部类。 In my application, I have a public class extending Application which holds some shared routines I want to access from different parts of my application. 在我的应用程序中,我有一个扩展Application的公共类,它包含了我想从我的应用程序的不同部分访问的一些共享例程。 When I break out the AsyncTask class however, I'm no longer able to call getApplicationContext() to get a reference to my application library (the class does not inherit anything from Activity now). 然而,当我打破AsyncTask类时,我不再能够调用getApplicationContext()来获取对我的应用程序库的引用(该类不会立即从Activity继承任何内容)。 getBaseContext() will have problems as well. getBaseContext()也会有问题。 Should I be passing a context into the AsyncTask when it gets instantiated and build from there? 我是否应该在实例化并从那里构建时将上下文传递给AsyncTask? Not sure if that was safe or not. 不确定这是否安全。
import java.util.ArrayList;
import android.os.AsyncTask;
public class DlTask extends AsyncTask
{
long totalbytes = 0;
long totalread = 0;
ArrayList <String> data;
@Override
protected void onPreExecute ()
{
AppLib lib = (AppLib) getApplicationContext();
lib.saveStatusText ("Contacting " + lib.getServerAddress () + ":" + lib.getServerPort () + "...");
super.onPreExecute ();
}
@Override
protected Object doInBackground (Object... params)
{
data = lib.sendCommand (CMD_LIST);
return true;
}
@Override
protected void onPostExecute (Object result)
{
if (data != null)
{
lib.saveStatusText (data.size () + " files found");
}
else
{
Log.d (TAG, "data is null");
Toast.makeText (getBaseContext(), msg, Toast.LENGTH_SHORT).show ();
}
super.onPostExecute(result);
}
}
Should I be passing a context into the AsyncTask when it gets instantiated and build from there? 我是否应该在实例化并从那里构建时将上下文传递给AsyncTask?
You do not have a choice, as you will be unable to get a Context
by any other means. 您没有选择权,因为您将无法通过任何其他方式获取Context
。
You should do like this. 你应该这样做。 In my case it works: 在我的情况下,它有效:
public class DlTask extends AsyncTask {
private Context context;
public DlTask(Context context) { this.context = context; }
@Override
protected void onPreExecute() {
super.onPreExecute();
..........
}
@Override
protected Object doInBackground (Object... params)
{
data = lib.sendCommand (CMD_LIST);
return true;
}
@Override
protected void onPostExecute (Object result)
{
if (data != null)
{
lib.saveStatusText (data.size () + " files found");
}
else
{
Log.d (TAG, "data is null");
Toast.makeText (context, msg, Toast.LENGTH_SHORT).show ();
//You can use the CONTEXT like this (passing the context variable like parameter
}
super.onPostExecute(result);
}
Either pass a Context in the constructor of your AsyncTask, or use a static variable in your Application class to access it (set sMyInstace = this;
). 在AsyncTask的构造函数中传递Context,或者在Application类中使用静态变量来访问它(设置sMyInstace = this;
)。 The first solution is preferable though. 但是第一种解决方案更可取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.