如何设置AsyncTask?

时间:2013-12-02 16:29:22

标签: android android-asynctask

我有queryAppIcon()方法,用于在数组appIconDrawable中查询和存储图像。但是,这项任务是永远的(即单击按钮查看图像,但在新屏幕出现之前我的应用程序暂停了5秒)所以我决定使用AsyncTask。但是,我正在调用一个没有参数的方法,所以我不能传递任何信息(即先ParseResult.get(0).get("Icon"),然后ParseResult.get(1).get("appIcon"), ..)。此外,没有信息作为我的{{传递回来1}}不等于任何东西,因为我不知道processFinish方法如何将信息传递给数组。我设置正确吗?我该怎么办?谢谢

appIconDrawable[i]

这是界面// global vars final Drawable[] appIconDrawable = null; int i; public Drawable[] queryAppIcon() throws ParseException, IOException { ParseQuery<ParseObject> query = ParseQuery.getQuery("AndroidStoreContent"); query.whereExists("appIcon"); List<ParseObject> ParseResult = query.find(); // initialize Drawable array final Drawable[] appIconDrawable = new Drawable[ParseResult.size()]; for (i = 0; i < ParseResult.size(); i++) { ParseFile pf = (ParseFile) ParseResult.get(i).get("appIcon"); startDownload(pf); } return appIconDrawable; } public void startDownload(ParseFile pf) { new DownloadImageTask(this).execute(pf); } public class DownloadImageTask extends AsyncTask<ParseFile, Void, Drawable> { private AsyncResponse ar; DownloadImageTask(AsyncResponse ar) { this.ar = ar; } @Override protected Drawable doInBackground(ParseFile... pf) { return fetchDrawable(pf[0]); } protected void onPostExecute(Drawable result) { ar.processFinish(result); } public Drawable fetchDrawable(ParseFile pf) { InputStream is; try { is = (InputStream) new URL(pf.getUrl()).getContent(); return Drawable.createFromStream(is,null); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } } @Override public void processFinish(Drawable d) { appIconDrawable[i] = d; // i also tried testing appIconDrawable[1] = d and the app loaded with all blank images and then crashes }

AsyncResponse

1 个答案:

答案 0 :(得分:0)

无论你传递给execute()的是doInBackground()中的参数,你从doInBackground返回的内容都将被传递给onPostExecute()。您还可以使用pubishProgress()和onProgressUpdate()来显示进度中发生的事情......

那是你在找什么?