我是Android开发的新手。有没有办法从非活动类中获取AsyncTask的结果?我知道使用接口和从onPostExecute
获取解析结果的标准过程。但这不起作用,在非活动类中使用上下文而不是activityname.this
(我将上下文作为参数发送)。我正在建立一个图书馆,这是必需的。任何帮助将不胜感激。
--->
Library l1 = new Library();
l1.init(portalHitter, "security certificate file name");
if(l1.getLoginStatus(MainActivity.this)){
start intent to go to another activity
}
在Library.java --->
CommonMethods commonMethods;
public void init(String portalHitter, String certName){
.....
}
public boolean getLoginStatus(Context context){
if(clint is initialized){
commonMethods = new CommonMethods(context, CommonValues.LOGIN_REQUEST);
}else{
/* error */
}
if(CommonValues.LOGIN_STATUS)
return true;
else
return false;
}
在CommonMethods.java --->
public CommonMethods(Context context, int reqest_code){
this.context = context;
this.request_for_which_service = reqest_code;
executeService();
}
public void executeService(){
switch(request_for_which_service){
case CommonValues.LOGIN_REQUEST:
loginAsyncTask = new LoginAsyncTask(context, params ...);
loginAsyncTask.execute();
loginAsyncTask.delegate = context;
break;
}
}
在LoginAsyncTask.java中--->
public class LoginAsyncTask extends AsyncTask<String, String, String>{
...
public LoginCompleteInterface delegate = null;
public LoginAsyncTask(Context context, params...){
...
}
doInBacground(){
..do work..
return response;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(exception_identifier == 0 || exception_identifier == 1 || exception_identifier == 2){
/* display dialog for exception on timeout, socket exceptions etc */
}else{
String tempLoginStatus = loginStatus(result);
delegate.loginCompleted(tempLoginStatus);
loginProgress.dismiss();
}
}
loginStatus(String result){
// parse and return success and failure //
}
...
}
并在LoginCompleteInterface.java ---&gt;
中public interface LoginCompleteInterface {
void loginCompleted(String output);
}
现在,如果commonmethods是一个活动,那么它不会产生问题,但现在它正在创建问题,即使在从MainActivity.java传递上下文之后
loginAsyncTask.delegate = context; line.
答案 0 :(得分:1)
您可以在Interface的帮助下获得。
<强>代码强> 接口
public interface MyInterface {
public void getResponse(String data);
}
活动类:
public class MainActivity extends AppCompatActivity implements MyInterface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// CAll Async Task
new AsyncTaskClass(this).execute();
}
@Override
public void getResponse(String data) {
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
}
AsyncTask类
public class AsyncTaskClass extends AsyncTask<String, String, String> {
private MyInterface mInterface;
public AsyncTaskClass(MyInterface reference) {
mInterface = reference;
}
@Override
protected String doInBackground(String... params) {
return "this data";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// call getResponse(_) of interface.
mInterface.getResponse(s);
}
}
----------------------对于非活动类--------------
public class SimpleClass implements MyInterface
{
public void someMethod()
{
// CAll Async Task
new AsyncTaskClass(this).execute();
}
}
接口和AsyncTask类将是相同的。
答案 1 :(得分:1)