0

I have this problem in android. i have a main activity who calls a thread with this

   Runnable work = new Runnable() {

    public void run() {
        while (kill) {
            try {
                Thread.sleep(5000);
                connect();
                } catch (InterruptedException ex) {
                Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
};

kill its a public boolean in MainActivity. what can i do to save the thread so when i resume the activity i still can kill the thread?

James Black
  • 41,583
  • 10
  • 86
  • 166

2 Answers2

1

Why do you want to kill the thread in the resume?

Why not start the thread in the resume part, then put in a finally block to handle the fact that the connect() is interrupted, and then when it starts again you just need to reconnect and do the operation.

In the case of mobile devices, your best bet is to checkmark in some persistent storage where you are, so that if you are interrupted you can continue when the Activity is started again.

You have a high possibility of being interrupted so design for it.

The thread may be killed off by the OS so there is nothing to save and kill later.

If you really need to do something like this then start a RemoteService as that will run in a different process than your Activity and is more likely to stay around.

You may want to look at this question:

Android Remote Service

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166
  • i need the thread to run in background on the phone, so when the user return to the application(activity) can kill the thread just pressing a button. – daniel fari Jun 17 '11 at 02:09
0

I think you want to do something closer to what this solution proposes.

However, if you really want to keep doing it in a Thread, then I suggest you extend a new class from Thread and add a method called killMe(). This will modify the (now private) boolean kill flag. Then, in your onRetainNonConfigurationInstance() you can return this Thread and you can retrieve it again in onResume. If you return and activity has not been killed, then it's fine, you can just call killMe() on the existing Thread.

Example:

 @Override
  public Object onRetainNonConfigurationInstance() {
    return thread;
  }
Community
  • 1
  • 1
dmon
  • 30,048
  • 8
  • 87
  • 96
  • ok i tried with thread thing, but now i cannot save the thread to loaded on the resume :S – daniel fari Jun 20 '11 at 20:37
  • Hmmm how about some more info? What didn't work, specifically? – dmon Jun 20 '11 at 21:03
  • i dont know why onRetainNonConfigurationInstance() doesnt execute :S, can you giveme and example? – daniel fari Jun 22 '11 at 15:54
  • I've updated the answer with a sample, but, *when* are you expecting it to execute? It will only execute if the activity is killed (e.g. orientation change). Otherwise, it's not necessary to retain/restore. – dmon Jun 22 '11 at 17:08
  • i want execute the save when i press the back button and return to android apps menu, and when i click again on the application icon restores the application including the running thread. o there is another way to execute a single main activity that keeps the threads? btw thanks for all the help :D – daniel fari Jun 22 '11 at 18:28
  • Ah see, the back button doesn't "sleep" the activity, it plain kills it, so this won't work, unless you override the `onBackPressed` behavior. The back button should kill your thread as well. – dmon Jun 22 '11 at 18:29
  • and how i do to sleep the activity??. and wake it when i press the application icon?? – daniel fari Jun 22 '11 at 19:32