您好,感谢您的帮助。
我有一个method
调用 anAsyncTask
从网上检索一些数据的方法。
method
依次调用多次,因此启动了几个AsyncTasks 。
从每次启动时,method
我都需要从相关的 AsyncTask (而不是从之前或之后调用的其他 AsyncTask )中获取正确的结果。
非常感谢任何帮助。
编辑 编辑 编辑 编辑 添加了其余代码。
请注意:整个过程在Service中运行。
public static class UpdateService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int[] appWidgetIds = intent.getIntArrayExtra("widgetsids");
final int N = appWidgetIds.length;
AppWidgetManager manager = AppWidgetManager.getInstance(this);
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Log.e("","i="+Integer.toString(i)+ " di "+Integer.toString(N));
RemoteViews view = buildUpdate(getApplicationContext(),
appWidgetIds);
manager.updateAppWidget(appWidgetId, view);
}
return (START_NOT_STICKY);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
private static RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
RemoteViews updateViews = new RemoteViews(ctxt.getPackageName(),
R.layout.widget);
updateViews.setTextViewText(R.id.price1, getPrice(list.get(0)
.getSymbol()));
}
//THIS METHOD IS CALLED SEVERAL TIMES IN SEQUENCE <----
private static String getPrice(String symbol) {
String result="";
UpdateTaskPrice up = new UpdateTaskPrice();
up.execute(symbol, null, null);
//HERE I WANT THE RESULT FROM onPostExecute() <----
return result;
}
//THIS IS THE ASYNCTASK WHICH IS LAUNCHED SEVERAL TIMES
public class UpdateTaskPrice extends AsyncTask<String, Void, String> {
@Override
protected void onProgressUpdate(Void... progress) {
}
@Override
protected void onPostExecute(String result) {
//HERE I RECEIVE THE RESULT FROM doInBackground <----
//I NEED TO PASS IT BACK TO getPrice() <----
}
@Override
protected String doInBackground(String... symbol) {
String result = "";
DefaultHttpClient client = new DefaultHttpClient();
String srt = "";
String url = context.getString(R.string.urlaternativo).concat(
symbol[0]);
HttpGet getMethod = new HttpGet(url);
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
srt = client.execute(getMethod, responseHandler);
int inizio = srt.indexOf("<last data=\"");
int fine = srt.indexOf("\"/>", inizio + 12);
result = srt.substring(inizio + 12, fine);
} catch (Throwable t) {
// Log.e("ERROR", "ERROR", t);
}
//HERE I GET THE RESULT I WANT, AND PASS IT TO onPostExecute() <----
return result;
}
}