1

I need to read selected file from external storage in background and then using Universal Image Loader download images, while during the process of reading file from external storage or during the image download process user should have a option to cancel that process by choosing another file to read from external storage and then download images. I use IntentService to read file from external storage startService() while when I need to cancel the current reading process and start the process for the selected file,is that a right approach? or I need to use HandlerThread/ Service/Thread

private void startResService() {
   myServiceIntent = new Intent(activity, ResService.class);
   stopResService();
   mServiceIntent.putExtra("cancelDownload", false);
   startService(mServiceIntent);
}

 private void stopResService() {
    mServiceIntent = new Intent(this,ResService.class);
    mServiceIntent.putExtra("cancelDownload", true);
    startService(myServiceIntent);
}




 private void onHandleIntent(Intent intent) {
    Bundle bundle = intent.getExtras();
    boolean isCanceled = false;
    if (bundle != null) {
        isCanceled = bundle.getBoolean("cancelDownload");
    }

    if (!isCanceled) {
        readRes();
    }
}
I.S
  • 1,904
  • 2
  • 25
  • 48
  • 1
    It depends on few thing, if you need to show downloaded image right away on UI you can go with AsyncTask since it a little bit easier (less work) to work with UI thread. If you need to do your work in background even if user closes (or other event cause putting your app in background) then you should go with IntentService – DoubleK Feb 20 '16 at 20:19
  • When app reads the file it should not show anything on UI thread,while during the process of image download ,which is implemented by UIL it should be shown the process of image download count. If the app goes background it should stop , while coming back it should continue the process, if the app is not killed. @DoubleK – I.S Feb 21 '16 at 10:11
  • This download count progres is shown, so that means that you have interaction with UI, but since you need pausing/resuming download it will probably be easier to use IntentService than AsyncTask, although pausing/resuming will give you more work than just downloading in background even if your app goes to background – DoubleK Feb 21 '16 at 10:27
  • the above methods are the right way for implementing start/stop service ? and for showing the count process from UIL listener I use BroadcastReceiver. @DoubleK – I.S Feb 21 '16 at 10:33
  • Yes, well startResService and stopResService will be in some other class, some activity right? Otherwise you can't call them as private, but everything else should be fine. And you will probably need to use class member variable inside readRes or pass it as param because code inside readRes will be inside another thread that read file and download image, and it won't stop only with this if(!isCancelled), this will just prevent from starting same thing again. I hope you understand what I wanted to say? – DoubleK Feb 21 '16 at 10:48
  • yes ,I call this methods from activity, but I check by isCancelled, I cancel UIL download and reading file from SDCard in onHandleIntent() ( I didn't post that part), as I know IntentService do it's job in another thread by default,I don't create thread in IntentService for reading file and UIL download images async . what is wrong in this approach? @ DoubleK – I.S Feb 22 '16 at 21:05
  • Nothing, better and easier for you :) – DoubleK Feb 22 '16 at 21:18

0 Answers0