Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Running task in background in android

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to run a long-running operation on a background thread without blocking UI thread. So how can I do it? I am new with Threads in java. The thread will be a periodic thread that will run after every 10sec. I wanted to know what. is the best option to do so. As a periodic Work manager minimum time interval is of 15 min and Async Task is ideal for short work which needs to be done on Background. So how could this be achieved?
 
Saloon Keeper
Posts: 6889
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it runs for less than 10 seconds, go with AsyncTask.

WorkManager could work as well, if each task schedules the next when it's done, rather than tasks being scheduled every X seconds or minutes.
 
priyanshi bhardwaj
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:If it runs for less than 10 seconds, go with AsyncTask.

WorkManager could work as well, if each task schedules the next when it's done, rather than tasks being scheduled every X seconds or minutes.



Let me explain my use case. I have an application that needs to maintain it's a connection with the server and then exchange messages. So, I want to do a connection establishment in ASYNC Manner via background thread. Doing it. in Sync with Ui Thread it has resulted into multiple ANR's
 
priyanshi bhardwaj
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:If it runs for less than 10 seconds, go with AsyncTask.

WorkManager could work as well, if each task schedules the next when it's done, rather than tasks being scheduled every X seconds or minutes.



It runs for more than 10 sec on weak network connection so can't go with AsyncTask. I need to do a connection check every X seconds. So with my use case which is the best way to achieve it without troubling the UI?
 
Tim Moores
Saloon Keeper
Posts: 6889
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
10 seconds was just an arbitrary number because you mentioned it. AsyncTask runs for longer than that just fine.

Doing networking on the UI thread simply won't work, that's basic Android programming. If you were using StrictMode -and you should- it would prevent that.
 
priyanshi bhardwaj
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:10 seconds was just an arbitrary number because you mentioned it. AsyncTask runs for longer than that just fine.

Doing networking on the UI thread simply won't work, that's basic Android programming. If you were using StrictMode -and you should- it would prevent that.



@Tim.  

I am actually not aware of how long the execution will last. So let's go with my use case if I want to take care of the connection on the background thread what should I use in Android so that I don't receive any anr?
The above thread will have two responsibilities :

1. Constantly keep on checking whether it got disconnected from the server or not.
2. If disconnected then establish the connection and if it is connected then  stay idle

What is the best way to do it?
 
Tim Moores
Saloon Keeper
Posts: 6889
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AsyncTask should be fine for that, as I already said. It is unusual for a mobile app to be permanently connected to a server, and not a good idea, though. Why do you think you need to do that?
 
priyanshi bhardwaj
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:AsyncTask should be fine for that, as I already said. It is unusual for a mobile app to be permanently connected to a server, and not a good idea, though. Why do you think you need to do that?



So that connection always stays alive. Like in wtsapp if internet is on then every time person can send a message. I was reading about Async Tasks got to know that they. create memory leaks too.
 
Master Rancher
Posts: 4251
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This link discusses using Threads:  https://developer.android.com/training/multiple-threads
 
priyanshi bhardwaj
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:-- deleted --



Norm I have mentioned mine use case I don't know services will be good or not? I never worked with threads before in android.
 
Tim Moores
Saloon Keeper
Posts: 6889
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the app has something to tell the server, that's what REST is for. For server to client communications you can use Firebase client messaging, or whatever it's called these days. As I said, keeping the connection open is a bad idea.
 
Tim Moores
Saloon Keeper
Posts: 6889
163
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no need to keep a connection open merely to be able to send a message at all times.
 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do this by using the Async Task !
 
Grow your own food... or this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic