Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Studio异步任务结果函数_Java_Android_Android Studio_Oop - Fatal编程技术网

Java Android Studio异步任务结果函数

Java Android Studio异步任务结果函数,java,android,android-studio,oop,Java,Android,Android Studio,Oop,我有一个类SysCallextendsAsyncTask 建造商: public SysCall(Context context,String [] attr,String [] value,String req_page) 呼叫时使用: String str_result= new SysCall(this,attr,value,"test.php").execute().get(); 然后我开始分析字符串stru result,但等待它并不好,它会冻结UI。 如果我的活动类中有一个方法

我有一个类
SysCall
extends
AsyncTask

建造商:

public SysCall(Context context,String [] attr,String [] value,String req_page)
呼叫时使用:

String str_result= new SysCall(this,attr,value,"test.php").execute().get();
然后我开始分析字符串
stru result
,但等待它并不好,它会冻结UI。


如果我的活动类中有一个方法,我喜欢在它完成工作时使用它,它可以访问所有的类变量,那么我该怎么做呢?

我从您的问题中了解到,您想在
asyncTask
完成后台工作后调用
活动中的一个方法:

class MainActivity extends .........{
......
......
......

//when you start the asyncTask:

new SysCall(this,attr,value,"test.php",this).execute();


//lets say you have a method called, callActivityMethod

public void callActivityMethod(){

 ....................

}



}
SysCall extends AsyncTask,,,,,,,,,,,,

...................
private MainActivity activity;


public SysCall(Context context,String [] attr,String [] value,String req_page, MainActivity activity){

...........
this.activity = activity;


}


//call the method from here
@Override
protected void onPostExecute(........) {
//this is called after your task is finished

//call the activity method
activity.callActivityMethod();
}
为此,必须将活动传递给asyncTask的构造函数:

class MainActivity extends .........{
......
......
......

//when you start the asyncTask:

new SysCall(this,attr,value,"test.php",this).execute();


//lets say you have a method called, callActivityMethod

public void callActivityMethod(){

 ....................

}



}
SysCall extends AsyncTask,,,,,,,,,,,,

...................
private MainActivity activity;


public SysCall(Context context,String [] attr,String [] value,String req_page, MainActivity activity){

...........
this.activity = activity;


}


//call the method from here
@Override
protected void onPostExecute(........) {
//this is called after your task is finished

//call the activity method
activity.callActivityMethod();
}
假设您的
活动
被称为
main活动
(可以是任何其他名称):

class MainActivity extends .........{
......
......
......

//when you start the asyncTask:

new SysCall(this,attr,value,"test.php",this).execute();


//lets say you have a method called, callActivityMethod

public void callActivityMethod(){

 ....................

}



}
SysCall extends AsyncTask,,,,,,,,,,,,

...................
private MainActivity activity;


public SysCall(Context context,String [] attr,String [] value,String req_page, MainActivity activity){

...........
this.activity = activity;


}


//call the method from here
@Override
protected void onPostExecute(........) {
//this is called after your task is finished

//call the activity method
activity.callActivityMethod();
}
在异步任务中,传递
MainActivity
引用:

class MainActivity extends .........{
......
......
......

//when you start the asyncTask:

new SysCall(this,attr,value,"test.php",this).execute();


//lets say you have a method called, callActivityMethod

public void callActivityMethod(){

 ....................

}



}
SysCall extends AsyncTask,,,,,,,,,,,,

...................
private MainActivity activity;


public SysCall(Context context,String [] attr,String [] value,String req_page, MainActivity activity){

...........
this.activity = activity;


}


//call the method from here
@Override
protected void onPostExecute(........) {
//this is called after your task is finished

//call the activity method
activity.callActivityMethod();
}

这很有帮助,但是如果我必须在不同的类中调用它,那么我应该在每个类中创建相同的方法名称呢?不同的类是什么意思?或者我也可以使用
Interface
?只需在
activity2.java
中创建另一个asynctask,并按照我告诉您的做同样的事情,或者您可以使用Interface。但这超出了问题的范围。如果我的回答有帮助,那么你就有了解决办法。问一个新问题,而不是你的问题。如果答案解决了你的特定问题,那么接受答案,让别人知道。祝你好运。