在不同的类中触发的异步任务和在不同的类中实现的回调函数
我是android和java的新手.我一直在android studio中做一个项目,其中我正在使用Asynctask提取用户的位置,并且有一个回叫界面来获取位置数据.
I am new to android and java. i have been doing a project in android studio in which i am using Asynctask to extract Location of user and there is a Call back interface to get Location data.
我看过有关asyntask的其他文章,但那里的asynctask在MainActivity中触发,并且接口也在同一类中实现. 但是我的项目有4个不同的类
I have looked in to other post regarding asyntask but there the asynctask is triggered in MainActivity and interface is also implemented in that same class. But mine project have 4 different class
1)data_extractor:在此Asynctask中被触发.
1)data_extractor: in this Asynctask is triggered.
2)Async_task:异步任务已在此处完成.
2)Async_task : The asynctask is done here.
3)ICallBack:回叫界面.
3)ICallBack : Call back interface.
4)DataCollector:此类已实现ICallBback接口,它将数据存储在DB中.
4)DataCollector : This class has Implemented the ICallBback interface and it will store data in DB.
我无法在我的datacollector类中获取loc_data.而且ICallBack也就是接口正在重设null.
I am not able to get loc_data in my datacollector class.and also ICallBack i.e is an Interface is returing null.
data_extractor.java
static Context _context = null;
static ICallBackInterface iCallBackInterface;
new IPAsyncTask(iCallBackInterface,_context).execute(struct_data); //fpstruc is class to structure my data
Async_Task.java
public class Async_Task extends AsyncTask<Struct_Data, Void, String> {
private ICallBackInterface iCallBackInterface;
Context context = null;
String data;
Struct_data data_struct= null;
public Async_Task(ICallBackInterface iCallBackInterface,Context context){
this.iCallBackInterface=iCallBackInterface;
this.context=context;
}
@Override
protected String doInBackground(Struct_Data... data_struct1) {
String res = "GPS Data";
return res;
}
@Override
protected void onPostExecute(String result) {
try {
String loc=LocationExtractor.addLocation(context, data_struct); //LocationExtractor is returning value.
iCallBackInterface.OnApiCallBAckReceived(loc);
}
catch(Exception e)
Log.e("Error",e);
}
}
public interface ICallBackInterface {
void OnApiCallBAckReceived(String loc_data);
}
public class DataCollector implements ICallBackInterface{
Context context;
String data;
@Override
public void OnApiCallBAckReceived(String loc_data) {
Log.d("CallBackData","Location=>"+loc_data)
}
}
您可以做的是创建一个侦听器接口,该接口将具有一个抽象方法,当后台任务完成时,异步类将调用该抽象方法.
What you can do is create a Listener interface which will have one abstract method which the async class will called when the background task is completed.
public interface AsyncTaskCompletedListener{
void taskCompleted();
}
在您的asynctask中,您还必须注册此Activity以获取回调.所以最好的方法是在asynctask的构造函数中传递活动的上下文
In your asynctask you also have to register this Activity in order to get the callback. So the best way is to pass the activitie's context in the constructor of asynctask
现在在您需要回调的活动中实现此接口
Now implement this interface in the activities where you want this callback
class MyActivity implements AsyncTaskCompletedListener{
//your code here
MyAsyncTask task = new MyAsyncTask(this);
@Override
void taskCompleted(){
//your code
}
}
在您的asynctask中,创建一个AsyncTaskCompletedListener实例,并为其分配传递上下文的值
In your asynctask create a instance of AsyncTaskCompletedListener and assign it the value of passed context
MyAsyncTask extends AsyncTask<Void,Void,Void>{
AsyncTaskCompletedListener listener;
public MyAsyncTask(AsyncTaskCompletedListener listener){
this.listener = listener;
}
@Override
public Void onPostExecute(){
this.listener.taskCompleted();
}
}
将要传递的任何数据传递给taskCompleted方法(您必须相应地修改接口).我只是给你一个一般的方法.根据需要对其进行修改.
Pass whatever data you want to pass to the taskCompleted method(You have to modify the interface accordingly). I just gave you a general method. Modify it as you want.