Android AsyncTask等到Status.FINISHED

时间:2014-03-17 20:50:56

标签: android android-asynctask dialog

我正在使用AsyncTask从url下载图像, 我希望在下载图像后立即在对话框中显示此图像。 我尝试使用此代码:它不会等到完成后,此代码将返回RUNNING。

if(dt.getStatus() == AsyncTask.Status.FINISHED) {

        dialog.show();

    }

2 个答案:

答案 0 :(得分:0)

您需要设置一个事件监听器。只需设置一个if-then语句就意味着它会在找到代码的那一刻被执行。事件侦听器将一直等到事件发生(在本例中为Status.FINISHED),然后执行侦听器内的代码。

答案 1 :(得分:0)

Ken Wolf帮助..

        public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
        private Context mcontec;

        public DownloadImageTask(ImageView bmImage, Context mContextr) {
            this.bmImage = bmImage;
            mcontec=mContextr;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {

            dialog(result, mcontec);
        }

        public void dialog(Bitmap resim, final Context ctx){

            final Dialog dialog = new Dialog(ctx, R.style.CustomDialogTheme);

            dialog.setContentView(R.layout.perdereklam);
            ImageView reklam = (ImageView) dialog.findViewById(R.id.reklampng);
            reklam.setImageBitmap(resim);
            reklam.setOnTouchListener(new OnTouchListener() {

            // fill your stuff...

    }

        dialog.show();
}

从任何班级打电话:

public static void showRateDialog(final Context mContextr) {
    final Dialog dialog = new Dialog(mContextr, R.style.CustomDialogTheme);
    ImageView reklam = (ImageView) dialog.findViewById(R.id.reklampng);
    final DownloadImageTask dt = new DownloadImageTask(reklam, mContextr);
            dt.execute("your url");
}