我有一个带有接口的Asynctask类,该类在完成后会回调到另一个类。问题是,一旦我调用了asynctask,它将根据onpostexecute上设置的代码每5s重新执行一次,而不管当前活动是什么。问题是我需要访问当前活动的接口,而不是启动Asynctask的接口。
我正在尝试创建一个变量Context contexto并将其更新为当前活动的上下文(每个活动的onCreate方法都具有MainActivity.contexto = this;
。我想将onpostexecute上的回调更改为当前活动活动,而不是起源于所有活动的活动,这样我就可以从该活动的界面访问输出。
这是到目前为止我的MainActivity内容:
public class MainActivity extends AppCompatActivity implements ClimateAPI.OnDownloadComplete, DownloadArduino.OnArduinoDownloadComplete,LuzPopup.ExampleDialogListener {
public static Context contexto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
DownloadArduino downloadArduino = new DownloadArduino(MainActivity.this);
downloadArduino.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public void onArduinoDownloadComplete(String temp, String humi, String lum, String light) {
Log.d(TAG, "onArduinoDownloadComplete: entrou ");
TextView textTempArd = (TextView) findViewById(R.id.textTempArd);
textTempArd.setText(temp+"°C" );
TextView textUmidArd = (TextView) findViewById(R.id.textUmidArd);
textUmidArd.setText(humi+ "%");
TextView textLumArd = (TextView) findViewById(R.id.textLumArd);
textLumArd.setText(lum);
if((DownloadArduino.lightstate==1) && (DownloadArduino.luminosity<300) && (contexto==this)) {
LuzPopup luzPopup = new LuzPopup();
luzPopup.show(getSupportFragmentManager(), "example dialog");
}
}
这就是我的AsyncTask中的内容:
public class DownloadArduino extends AsyncTask<String,Void,Void> {
private static final String TAG = "DownloadArduino";
public int internetAccess = 0; /*Variavel que checa acessibilidade da internet ( 0 = inacessivel, 1 = acessivel )*/
public static String umidade;
public static String temperatura;
public static String luminosidade;
public static int lightstate;
public static int luminosity;
public static String light;
public String result;
private final OnArduinoDownloadComplete mCallBack;
interface OnArduinoDownloadComplete{
void onArduinoDownloadComplete(String temp, String humi, String lum, String light);
}
public DownloadArduino(OnArduinoDownloadComplete callBack) {
Log.d(TAG, "DownloadArduino: callback e" + callBack);
mCallBack = callBack;
}
@Override
protected void onPostExecute(Void aVoid) {
Log.d(TAG, "onPostExecute: contexto e " + MainActivity.contexto);
if (internetAccess == 0 ){ /*Output para o caso de nao haver conexao desde o comeco do metodo*/
Toast.makeText(MainActivity.contexto, "Verifique a conexão", Toast.LENGTH_LONG).show(); /*Nao ha conexao*/
}
mCallBack.onArduinoDownloadComplete(temperatura,umidade,luminosidade,light);
if (MainActivity.logado) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
DownloadArduino downloadArduino = new DownloadArduino((OnArduinoDownloadComplete) MainActivity.contexto);
downloadArduino.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}, 5000);
}
}
问题是我无法将上下文应用于回调,是否有办法做到这一点,以便被回调的活动始终是当前活动(而不是调用asynctask的活动)?