Android / Glass:如何触发surfaceDestroyed

时间:2014-01-22 15:20:41

标签: android multithreading surfaceview google-glass surfaceholder

嘿大家我对android / glass开发相当新,所以如果我错了请纠正我。

我试图创建一个很好的高频现场卡。但是,当我关闭应用程序时,函数surfaceDestroyed()没有获得触发器,并且没有调用thread.quit()。

我试着查看示例项目秒表/计时器,显然他们也没有停止线程。根据文档,surfaceDestroyed在表面被破坏之前被调用,但是我的表面是什么?我也读过一些调用surfaceview的东西,它们是一样的吗? surfaceview我的customview或livecard?

https://developers.google.com/glass/develop/gdk/ui/live-cards#creating_high-frequency_live_cards

欣赏任何帮助!!!

/ ***
  * LiveCardRender Class
*** /
public class LiveCardRender implements DirectRenderingCallback {
    private static final long FRAME_TIME_MILLIS = 33;
    private CustomView mCustomView;
    private SurfaceHolder mHolder;
    private boolean mPaused;
    private RenderThread mRenderThread;

    private class RenderThread extends Thread {
        private boolean mShouldRun;

        public RenderThread() {
            mShouldRun = true;
        }

        private synchronized boolean shouldRun() {
            return mShouldRun;
        }

        public synchronized void quit() {
            mShouldRun = false;
        }

        @Override
        public void run() {
            while (shouldRun()) {
                draw(mCustomView);
                SystemClock.sleep(FRAME_TIME_MILLIS);
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mHolder = holder;
        updateRendering();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mHolder = null;
        updateRendering();
    }

    @Override
    public void renderingPaused(SurfaceHolder holder, boolean paused) {
        mPaused = paused;
        updateRendering();
    }

    private synchronized void updateRendering() {

        boolean shouldRender = (mHolder != null) && !mPaused;
        boolean rendering = mRenderThread != null;

        if (shouldRender != rendering) {
            if (shouldRender) {
                mRenderThread = new RenderThread();
                mRenderThread.start();
            } else {
                mRenderThread.quit();
                mRenderThread = null;
            }
        }
    }

    private void draw(View view) {

        Canvas canvas;
        try {
            canvas = mHolder.lockCanvas();
        } catch (Exception e) {
            return;
        }
        if (canvas != null) {
            view.draw(canvas);
            mHolder.unlockCanvasAndPost(canvas);
        }
    }
}

/ ***
  * LaunchService Class that uses LiveCardRender to update the live card
*** /
public class LaunchService extends Service {
    private static final String LIVE_CARD_TAG = "motion_card";
    private TimelineManager mTimelineManager;
    private LiveCard mLiveCard;
    private LiveCardRender mLiveCardRender;

    @Override
    public void onCreate() {
        mTimelineManager = TimelineManager.from(this);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mLiveCard == null) {
            mLiveCard = mTimelineManager.createLiveCard(LIVE_CARD_TAG);
            mLiveCardRender = new LiveCardRender(this);
            mLiveCard.setDirectRenderingEnabled(true);
            mLiveCard.getSurfaceHolder().addCallback(mLiveCardRender);
            mLiveCard.publish(PublishMode.REVEAL);
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy(){
        if (mLiveCard != null && mLiveCard.isPublished()) {
            if (mLiveCardRender != null) {
                mLiveCard.getSurfaceHolder().removeCallback(mLiveCardRender);
            }
            mLiveCard.unpublish();
            mLiveCard = null;
        }
        super.onDestroy();
    }
}

1 个答案:

答案 0 :(得分:1)

onDestroy内部看起来好像是错误:

if (mLiveCard != null && mLiveCard.isPublished()) {
    if (mLiveCardRender != null) {
        mLiveCard.getSurfaceHolder().removeCallback(mLiveCardRender);
    }
    mLiveCard.unpublish();

通过在调用unpublish方法之前删除回调,系统不再知道在之后删除卡时要调用的surfaceDestroyed方法。您实际上可以完全删除对removeCallback的呼叫;这是不必要的。

看起来这是我们的一些样本中的一个错误,我们在API发展过程中错过了这个错误。感谢您抓住它,我们很快就会对它们进行更新!