大家好,我是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();
}
}
最佳答案
看来错误在这里,在onDestroy
内:
if (mLiveCard != null && mLiveCard.isPublished()) {
if (mLiveCardRender != null) {
mLiveCard.getSurfaceHolder().removeCallback(mLiveCardRender);
}
mLiveCard.unpublish();
通过在调用
unpublish
方法之前删除回调,系统在以后删除卡时不再知道要调用哪个surfaceDestroyed
方法。您实际上可以完全删除对removeCallback
的调用;没必要看起来这是我们的某些示例中的错误,但随着我们的API的发展,我们已经错过了它们。感谢您抓住它,我们会尽快对其进行更新!