首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在onDestroy() android Studio中停止blink方法?

如何在onDestroy() android Studio中停止blink方法?
EN

Stack Overflow用户
提问于 2019-03-27 14:25:39
回答 1查看 70关注 0票数 0

我想停止onDestroy()中的处理程序。代码如下。blink()方法在活动中出于特定原因调用,但希望在销毁方法中停止它们的服务。

代码语言:javascript
复制
final Handler handler = new Handler();
private void blink() {
    PrintLog.log("On", "Blink Thread");
    new Thread(new Runnable() {
        @Override
        public void run() {
            int timeToBlink = 1000;    //in milissegunds
            try {
                Thread.sleep(timeToBlink);
            } catch (Exception e) {
            }
            handler.post(new Runnable() {
                @Override
                public void run() {

                    if (text_ATMCardInstruction.getVisibility() == View.VISIBLE) {
                        text_ATMCardInstruction.setVisibility(View.INVISIBLE);
                    } else {
                        text_ATMCardInstruction.setVisibility(View.VISIBLE);
                    }
                    blink();
                }
            });
        }
    }).start();
}

@Override
protected void onDestroy() {

    // what is code here?
    PrintLog.log("Stop", "serviceStop");
    super.onDestroy();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-29 15:13:39

treadRunning布尔值onDestroy()方法的句柄设置treadRunning = false;

代码语言:javascript
复制
private void blink() {
    PrintLog.log("On", "Blink Thread");

    if (treadRunning) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int timeToBlink = 1000;    //in milissegunds
                try {
                    Thread.sleep(timeToBlink);
                } catch (Exception e) {
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (text_ATMCardInstruction.getVisibility() == View.VISIBLE) {
                            text_ATMCardInstruction.setVisibility(View.INVISIBLE);
                        } else {
                            text_ATMCardInstruction.setVisibility(View.VISIBLE);
                        }
                        blink();
                    }
                });
            }
        }).start();
    } else {
        PrintLog.log("On", "Blink Thread Stop");
        new Thread(new Runnable() {
            @Override
            public void run() {
                text_ATMCardInstruction.setVisibility(View.INVISIBLE);
            }
        }).interrupt();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55371040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档