0

Am creating an app in which background service decrypt downloaded file in the folder and will show file the file. Once user click on selected file it will start decrypting, if user click on that button decryption will stop and will start with new file. I tried using thread for to start and stop this task.

if (mUpdate!=null) {
             mUpdate.interrupt();
         }

         mUpdate=new Thread() {
             @Override
             public void run() {
                 while (!interrupted()) {
                     // decryption of file and showing file to user
                 }
             }
         };
         mUpdate.start();
  • added code, please –  Feb 02 '17 at 14:52
  • Wouldn't it be easier if you use an AsyncTask or an IntentService? There is plenty of documentation on android developer about those – Nicolás Carrasco-Stevenson Feb 02 '17 at 14:54
  • am using this inside an service –  Feb 02 '17 at 14:56
  • @BincyBaby Creating threads more than you need is a bad practice. Creating of a new thread is costly. The solution I posted in another question you have asked may be helpful here also. That solution has the design needed for pausing a thread and then resuming the same. The link is http://stackoverflow.com/a/42049397/504133 – nits.kk Feb 05 '17 at 07:41

1 Answers1

0

I would write something like that if you want checking if thread is stopped in while loop:

class StoppableThread extends Thread {
    private AtomicBoolean mIsStopped = new AtomicBoolean(false);
    public void stopThread() {
        mIsStopped.set(true);
    }
    public boolean isThreadStopped() {
        return mIsStopped.get();
    }
}
RadekJ
  • 2,835
  • 1
  • 19
  • 25