1

I have to make an app where I have to show a map (I'm using Osmdroid) and read a text file that has a list of geographic coordinates. I have to read a pair of latitude and longitude and show it on the map, and after one second read the next pair and show it on the map, and so on.

I was thinking of reading the file in a Thread, but I found that I can't do anything with the user interface in a Thread, so I guess I wouldn't be able to show the location on the map.

Then I found that with an AsyncTask I could modify the user interface but in the documentation says that it is for short tasks, and what I want to do may take many minutes.

In the AsyncTask documentation says If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask, but I don't know when to use those classes and if one of them (or any other) would help me to do what I want: read a line of the text file and show the location on the map.

Adrián Juárez
  • 183
  • 5
  • 14
  • I suggest you to use RxJava. Just a few lines of code can solve your problem. And also you don't have to worry about threading and delay interval calculation. Here is a example SO question about that. I know it is not the same situation but maybe it can give you an idea.http://stackoverflow.com/questions/33291245/rxjava-delay-for-each-item-of-list-emitted – mertsimsek Mar 09 '17 at 21:05

2 Answers2

1

You can use AsyncTask Class for loading data in background and update the UI Easly Or Try using Loaders as well it's efficient Than AsyncTask read this Links and search youtube for AsyncTask or Loaders as well

See this example at

StackOverFlow

And read the official Documentation here and here

Community
  • 1
  • 1
Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36
0

If you fetch the data from another thread, perhaps with AsyncTask, then you're responsible for managing both the thread and the UI thread through various activity or fragment lifecycle events, such as onDestroy() and configurations changes use Loaders

Glorfindel
  • 21,988
  • 13
  • 81
  • 109