Subscribe or bind to an existing Intent service

I have an application that has an initial activity that lists some files in a list. When an item is clicked on a list, it moves on to the detailed action of that particular file.

In the detailed view, I have a button called download, when you click on download, it launches IntentService, which sets the file to load as such:

downloadButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(AppDetailsActivity.this, AppDownloadService.class); intent.putExtra(Consts.APP_DOWNLOAD_RECEIVER_KEY, new DownloadReceiver(new Handler())); startService(intent); } }); 

The DownloadReceiver class is used to update the progress bar in the detailed description of the downloaded file.

Now what I want to do ...

If the user is currently downloading a file from a detailed action and then returning to the previous action, I want the activity to somehow sign / bind to the same IntentService and retrieve the results so that it can display a progress bar in the list item of the downloaded file.

  • Is this possible with IntentService?
  • Is IntentService correct?
  • Are there any examples (since I did not find anything that would show me how to do this)?
+4
source share
2 answers

You can use IntentService . He works on a different topic. Also, keep in mind that when you call startService() several times for this IntentService it will create a queue and will process one intent at a time.

To show the progress on Activity , I do not believe in translation, this is correct. Although it is very likely that this process is in order, the infrastructure itself or Android does not guarantee an order on delivery. This means that you can get broadcast at 50%, and then broadcast at 40%. For this, I recommend you take a look at this example: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

It may look a little more complicated, but you will have a two-way communication that you can rely on.

+8
source

I managed to find a solution. It turns out that I should use the intention of the broadcast to publish progress in my service; And use BroadcastReceiver in my actions to get results.

The link below shows an example of sending and receiving results from services through broadcasts.

http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/

0
source

Source: https://habr.com/ru/post/1445502/