Java Android - 检查 Runnable 是否正在运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23487754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:20:20  来源:igfitidea点击:

Android - Check if Runnable is running

javaandroideclipsemultithreadingservice

提问by beLejer

I want to check if either the Runnable is alive or not in my service. Is there a way i could do this?

我想检查 Runnable 在我的服务中是否还活着。有没有办法做到这一点?

private Runnable sendData = new Runnable() {
    public void run() {
        try {
            superToast.show();
            screenOn();
            vibrate();
            mHandler.postDelayed(sendData, time);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

采纳答案by Waqar Ahmed

you can create a boolean variable and check its value whether it is running or not

您可以创建一个布尔变量并检查其值是否正在运行

boolean isRunning = false;

private Runnable sendData = new Runnable() {
    public void run() {
        try {
            isRunning = true;
            superToast.show();
            screenOn();
            vibrate();
            mHandler.postDelayed(sendData, time);
        } catch (Exception e) {
            e.printStackTrace();
            isRunning = false;
        }
    }
};

and now check for isRunning, if its true it is running else not running

现在检查isRunning,如果它是真的,它正在运行,否则没有运行