如何在AsyncTask使用另一个Thread时设置延迟时间?

时间:2014-04-22 10:13:27

标签: java android multithreading android-asynctask

我想为扩展AsyncTask的类设置延迟时间(以毫秒为单位)。但是这个类也使用了另一个java Thread。有一些类可以在Android上流式传输Mjpeg,只需对此问题进行一些更改:Android ICS and MJPEG using AsyncTask

这是我的类,它扩展了AsyncTask:

public class GetCameraMjpeg extends AsyncTask<String, Void, MjpegInputStream> {

    @Override
    protected MjpegInputStream doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            if (username != null && password != null) {
                String authEncoded = Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT);
                connection.setRequestProperty("Authorization", "Basic " + authEncoded);
            }
            connection.connect();
            return new MjpegInputStream(connection.getInputStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(MjpegInputStream result) {
        mjpegView.setSource(result);
        mjpegView.setDisplayMode(MjpegView.SIZE_FULLSCREEN);
    }
}

MjpegInputStream类与上面粘贴的Stackoverflow问题相同。 MjpegView是扩展SurfaceView的类。它包含内部类MjpegViewThread,它是扩展java Thread类的类。这是此线程的run方法。

@Override
public void run() {
    Bitmap bitmap;
    Rect destRect;
    Canvas canvas = null;
    while (mjpegRun) {
        if (surfaceDone) {
            try {
                canvas = surfaceHolder.lockCanvas();
                if (canvas != null) {
                    synchronized (surfaceHolder) {
                        try {
                            bitmap = mjpegInputStream.readMjpegFrame();
                            if (displayMode != SIZE_FULLSCREEN) {
                                displayWidth = bitmap.getWidth();
                                displayHeight = bitmap.getHeight();
                            }
                            destRect = destRect(bitmap.getWidth(), bitmap.getHeight());
                            canvas.drawColor(Color.TRANSPARENT);
                            canvas.drawBitmap(bitmap, null, destRect, new Paint());
                        } catch (IOException e) {
                            e.getStackTrace();
                            Log.d(TAG, "catch IOException hit in run", e);
                        }
                    }
                }
            } finally {
                if (canvas != null) {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

我尝试过这个答案:How to execute Async task repeatedly after fixed time intervals但没有明显的区别。我想是MjpegView运行自己的java Thread

1 个答案:

答案 0 :(得分:0)

使用处理程序     Handler h = new Handler();     h.postDelay(功能,时间);