在执行长时间的onDestroy运行的操作长时间、操作、onDestroy

2023-09-07 18:01:57 作者:醉梦人生

我有我需要在的onDestroy()我的活动的。什么是做到这一点的最好方法是什么?

I have a "long-running" cleanup operation that I need to perform in onDestroy() of my Activity. What is the best way to do this?

如果我使用来做到这一点,我的的onDestroy()将立即返回;但发生了什么主题的参考?我在寻找任何影响/陷阱/我需要知道这里跳闸电线的意见,因为我认为这个过程将仍然是活动被破坏后,即使还活着。

If I use a Thread to do this, my onDestroy() will return immediately; but what happens to the Thread reference? I am looking for advice on any implications/gotchas/trip-wires that I need to be aware of here since I assume that the process will still be alive even after the Activity is destroyed.

我使用 JmDNS 在我的应用程序。当用户与我的应用程序做的,我要清理JmDNS实例。我该使用 JmDNS 类的close()方法做。然而,这种方法需要的超过5秒以完成。这样一来,用户触摸返回键后看到我的活动在屏幕上很长一段时间。

I am using JmDNS in my app. When the user is done with my app, I want to clean up the JmDNS instance. I do this using the close() method of JmDNS class. However, this method takes more than 5 seconds to complete. As a result, the user sees my Activity on screen for a long time after touching the Back key.

我还没有弄清楚为什么的close()需要那么长,但在同时,我也意识到,我真的不需要等待近成功完成。所有我需要的是一种方法,触发的密切,并与它做。

I am yet to figure out why the close() takes that long, but in the meanwhile I also realized that I really don't need to wait for the close to complete successfully. All I need is a way to "trigger" the close and be done with it.

推荐答案

最后我做什么,我问过的问题 - 我开始执行长在的onDestroy -running操作()

I ended up doing what I had asked in the question - I start a Thread to perform the long-running operation in onDestroy().

当用户重新打开我的应用程序的长期运行已经完成甚至在一个情况下,我不得不考虑了。在我的应用程序,这意味着JmDNS的新实例被创建。所以,我在单独清理每个实例我的的onDestroy

One case I had to consider was when the user re-opens my app even before the long-running has completed. In my app, this means a new instance of JmDNS gets created. So, I clean up each instance separately in my onDestroy.

您使用的情况下可能有所不同 - 可能要启动清理线程,只有当它尚未运行(使用的isAlive() 法或类似技术)。

Your use case might differ - you might want launch the cleanup thread only if it is not already running (using Thread's isAlive() method or some such technique).

下面是一些示例code。美联社preciate了单独清理每个实例部分,执行以下步骤序列:

Here's some sample code. To appreciate the "clean up each instance separately" part, perform the following sequence of steps:

启动应用程序 preSS后退按钮。你会看到在LogCat中清理操作重新启动应用程序。

再次退出该应用程序。现在,你会看到两套清理日志 - 第一个重presenting清理第一个实例;和对应于第二个实例所述第二组 Launch the app Press the back button. You will see the clean up operation in LogCat Re-launch the app.

Again, exit the app. Now, you will see two sets of cleanup logs - the first one representing cleanup for the first instance; and the second set corresponding to the second instance.

public class DelayedExitActivity extends Activity {

    private static final String LOG_TAG = "DelayedExit";
    private final  Runnable longOperation = new Runnable(){
        @Override
        public void run() {
            for (int i=0 ; i < 50; i++){
                Log.d(LOG_TAG, "Iteration "+i);
                try {
                    Thread.sleep(2 * 1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
    private Thread longThread ;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onDestroy() {
        if(longThread == null){
            longThread = new Thread(longOperation);
        }
        longThread.start();
        super.onDestroy();
    }
}