我想通過它叫成的AsyncTask的的AsyncTask父活動..傳遞一個父活動對象轉換成的AsyncTask
下面是我的代碼的一部分,我想使用doinbackground參數,而不是字符串的活動對象,但我不能因爲我不能從頭做到這一點..請解釋如何做到這一點..
public class Backgroundtask extends AsyncTask<String, Void, String> {
AlertDialog alertDialog;
AlertDialog alertDialog2;
Context ctx;
Userlocalstore userlocalstore;
String res_name , res_username , res_dob , res_email , res_id , res_pass;
Backgroundtask(Context ctx)
{
this.ctx =ctx;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information....");
alertDialog2 = new AlertDialog.Builder(ctx).create();
alertDialog2.setTitle("Apologies");
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://8miles.freeiz.com/Register.php";
String login_url = "http://8miles.freeiz.com/getuserinfo.php";
String method = params[0];
if (method.equals("Register")) {
String name = params[1];
String user_name = params[2];
String user_pass = params[3];
String email = params[4];
String dob = params[5];
傳遞的活動進入一個AsyncTask可能有點危險 - 如果AsyncTask永遠不會結束,那麼這個Activity將被泄漏。無論哪種方式,即使在活動完成之後,任務仍會繼續佔用內存,直到任務結束。這包括輪換案件。在這裏小心點。 –
由於Gabe Sechan提到的原因,你可能不應該那樣做,但是你可以簡單地用Activity替換你的Context ctx,因爲Activity也是一個Context。 – BladeCoder