简体   繁体   English

如果调用取消或停止相同的功能,则重新启动(如果当前在Android中运行)

[英]if called Cancel or stop same function then start fresh if presently running in Android

How do I within Android, if a function is called which is already running, cancel/stop that function then start it from the beginning? 我如何在Android中,如果已经调用了一个正在运行的函数,请取消/停止该函数,然后从头开始?

My code looks like the below, basically throughout my application a given function( Important(); ) is called but rather than have that function run several times or prevent users from running the function again if presently running. 我的代码如下所示,基本上是在我的整个应用程序中调用了给定的函数(Important();),但是该函数没有运行几次,或者如果当前正在运行,则阻止用户再次运行该函数。 I'd like to Cancel/stop the currently running function, then start it fresh? 我想取消/停止当前正在运行的功能,然后重新启动它?

How can I go about doing this? 我该怎么做呢?

Code: 码:

public void Important() {
//Do lots of stuff
}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Important();
    }

    @Override
    protected void onResume() {
        super.onResume();
    Important();
    }

I've already attempted the below making use of booleans. 我已经尝试过以下使用布尔值的方法。 But it just stops a user from running the function again if already running? 但这只是阻止用户再次运行该功能(如果已运行)? (which isn't what I'm trying to do) (这不是我想做的)

I want, when the function is called again. 我想要当函数再次被调用时。 The running function is stopped. 运行功能已停止。 Then freshly executed 然后重新执行

Code: 码:

Boolean runningCode = false;
    public void Important() {
if (runningCode== false) { 
runningCode = true;
    //Do lots of stuff
runningCode = false;
    }

FYI FYI

Important() is called many times throughout my application ( in addition to the above ), sometimes whilst Important() is still running. 在我的应用程序中( 除上述内容之外 ), 多次调用 Important(),有时在Important()仍在运行时。 Hence the need for a solution to my question :-) 因此,有必要解决我的问题:-)

Tasks like resetting variables and formatting the page are carried out within Important(); 诸如重置变量和格式化页面之类的任务是在Important()中执行的; , it is not running on a separate thread. ,它不在单独的线程上运行。 These are tasks that need to be completed on the main UI thread 这些是需要在主UI线程上完成的任务

You can use a Handler on the UI thread and post messages or Runnables to it. 您可以在UI线程上使用Handler并向其发布消息或Runnables。 Make every discrete action in the Important() function be a message or Runnable containing a function (eg, resetVariables() , formatThePage() , etc). 使Important()函数中的每个离散操作都成为包含函数的消息或Runnable(例如resetVariables()formatThePage()等)。 Whenever you call the function again, just call removeCallbacksAndMessages on the Handler and it will drop all actions that have not yet been executed. 每当您再次调用该函数时,只需在Handler上调用removeCallbacksAndMessages ,它将删除所有尚未执行的动作。

Well, I don't understand why you want to do something like this? 好吧,我不明白你为什么要这样做?

There is not one simple answer to that. 没有一个简单的答案。 Probably working solution would be something like this 可能有效的解决方案将是这样的

public void Important() {
if (runningCode == false) { 
    runningCode = true;
    //Do lots of stuff
    runningCode = false;
} else {
    // stop current code from execution
    runningCode = false;
    Important();
}

But you must figure your own code to stop current execution as it may result in broken state of objects etc... 但是您必须弄清楚自己的代码才能停止当前执行,因为这可能导致对象等的损坏状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM