如果我将AsyncTask作为一个单独的类,并且我从一个Activity运行AsyncTask,那么AsyncTask是否会保持对Activity的引用,就像它是后者的内部类一样? 或者它是否解决了内存泄漏的问题?
另外,将WeakReference中包含的上下文传递给AsyncTask会进行任何更改吗?
答案 0 :(得分:-1)
你可以像这样使用并获得活动的反应
public class DeleteUserTask extends AsyncTask<String, String, String> {
private Context context;
private JSONObject Object, Object1;
public static String Success, Exception;
public static boolean isClick;
public DeleteUserTask(Context ctx) {
context = ctx;
}
@Override
protected void onPreExecute() {
OrganizationUser_EditProfileScreen.pb.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
try {
ActivityBase.result = null;
Log.e("", "Request-->" + writeJSON().toString());
ActivityBase.PostMethod(context,
WebServiceLinks.UserStatusButtonTask, writeJSON());
Object = new JSONObject(ActivityBase.result);
Object1 = Object
.getJSONObject("OrganizationUserProfileButtonUpdateResult");
Success = Object1.getString("Success");
Exception = Object1.getString("Exception");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
try {
OrganizationUser_EditProfileScreen.pb.setVisibility(View.GONE);
if (ActivityBase.result != null && Success == "true") {
ActivityBase
.GeneralDialog(R.string.account, Exception, context);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public JSONObject writeJSON() {
JSONObject object = new JSONObject();
try {
object.put("OrgID", OrganizationLoginTask.OrgID);
object.put("OrgUserID", Search_Screen.OrgUserID);
object.put("Status", OrganizationUser_EditProfileScreen.Status);
object.put("ModifiedByTypeCode", "2");
object.put("ModifiedSourceCode", "8");
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
}