0

我的应用程序中有一个活动,它需要几个屏幕捕获并通过单击按钮移动到下一个活动。我想在截屏时向用户显示一些进度或状态(需要 5-8 秒)。我尝试使用AsyncTaskwith runOnUithread()。但没有运气。即使我在 中编写了整个代码doInBackground(),我也不知道为什么在截取所有屏幕截图后会启动 ProgressDialog。这样做的更好方法是什么?这是我的代码:

public class TakeScreenShots extends AsyncTask<Void, Void, Void> {
        private ProgressDialog dialog;


            @Override
            protected void onPreExecute() {
                this.dialog = ProgressDialog.show(MyActivity.this, "MyTitle",
                        "Loading .....", true);

            }

            @Override
            protected Void doInBackground(Void... params) {
                 MyActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        takeScreenshots();

                    }
                }); 

                return null;
            }

            @Override
            protected void onPostExecute(Void unused) {
                gotoNextActivity();

                this.dialog.dismiss();

            }

        }
4

2 回答 2

1

不,你做错了,而是在runOnUiThread直接捕获它doInBackground()

这是工作示例

public class backImageProcess extends AsyncTask<Void, Void, Void>
    {
private ProgressDialog dialog;
        @Override
        protected void onPreExecute() {

            this.dialog = ProgressDialog.show(MyActivity.this, "MyTitle",
                        "Loading .....", true);

            try {
                File mFile= new File(mTempImagename);
                if(mFile!=null)
                    mFile.delete();
            } catch (Exception e) {
            }   

            super.onPreExecute();
        }
        @Override
        protected Void doInBackground(Void... params) {
            mLayoutRoot.setDrawingCacheEnabled(true);
            mLayoutRoot.buildDrawingCache();

            Bitmap mBitmap= mLayoutRoot.getDrawingCache();
            try {
                if(mBitmap!=null)
                {
                    FileOutputStream out = new FileOutputStream(mTempImagename);
                    mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                    out.flush();
                    out.close();
                }
            } catch (Exception e) { }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if(this.dialog!=null)
                this.dialog.dismiss();

            gotoNextActivity();

        }
    }
于 2012-11-27T10:43:07.333 回答
0

我从这个问题的答案中得到了答案。我正在为图像视图设置一个可绘制对象。所以,我将那条线保留在 Ui Thread 中,并保留在 doInBackground() 中。

于 2012-11-27T11:19:50.577 回答