Reputation: 1731
I want some code to run in the background continuously. I don't want to do it in a service. Is there any other way possible?
I have tried calling the Thread
class in my Activity
but my Activity
remains in the background for sometime and then it stops. The Thread
class also stops working.
class testThread implements Runnable {
@Override
public void run() {
File file = new File( Environment.getExternalStorageDirectory(), "/BPCLTracker/gpsdata.txt" );
int i = 0;
RandomAccessFile in = null;
try {
in = new RandomAccessFile( file, "rw" );
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//String line =null;
while ( true ) {
HttpEntity entity = null;
try {
if ( isInternetOn() ) {
while ( ( line = in.readLine() ) != null ) {
HttpClient client = new DefaultHttpClient();
String url = "some url";
HttpPost request = new HttpPost( url );
StringEntity se = new StringEntity( line );
se.setContentEncoding( "UTF-8" );
se.setContentEncoding( new BasicHeader( HTTP.CONTENT_TYPE, "application/json" ) );
entity = se;
request.setEntity( entity );
HttpResponse response = client.execute( request );
entity = response.getEntity();
i++;
}
if ( ( line = in.readLine() ) == null && entity != null ) {
file.delete();
testThread t = new testThread();
Thread t1 = new Thread( t );
t1.start();
}
} else {
Thread.sleep( 60000 );
} // end of else
} catch (NullPointerException e1) {
e1.printStackTrace();
} catch (InterruptedException e2) {
e2.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}// end of while
}// end of run
}
Upvotes: 172
Views: 290521
Reputation: 523
//Works accurately for me
new Thread( () -> {
//run code on background thread
activity.runOnUiThread(()->{
//update the UI on main thread
});
//here activity is the reference of activity
}).start();
//OR
new Thread(new Runnable() {
@Override
public void run() {
//run code on background thread
activity.runOnUiThread(()->{
//update the UI on main thread
});
//here activity is the reference of activity
}
})
Upvotes: 5
Reputation: 4411
Remember Running Background, Running continuously is two different tasks.
For long-term background processes, Threads aren't optimal with Android. However, here's the code, and do it at your own risk.
To approach it in the right way, you need to Start Service First, Inside the service, you need to start the Thread/Async task Which needs Runnable.
Remember Service and Thread will run in the background but our task needs to make triggers (call again and again) to get updates, i.e. once the task is completed we need to recall the function for the next update.
Timer (periodic trigger), Alarm (Timebase trigger), Broadcast (Event base Trigger), recursion will awake our functions.
public static boolean isRecursionEnable = true;
void runInBackground() {
if (!isRecursionEnable)
// Handle not to start multiple parallel threads
return;
// isRecursionEnable = false; when u want to stop
// on exception on thread make it true again
new Thread(new Runnable() {
@Override
public void run() {
// DO your work here
// get the data
if (activity_is_not_in_background) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// update UI
runInBackground();
}
});
} else {
runInBackground();
}
}
}).start();
}
Using Service: If you launch a Service it will start, It will execute the task, and it will terminate itself after the task execution if the service is not STICKY. This Service termination might also be caused by an exception in code or the user killed it manually from settings.
START_STICKY (Sticky Service) is the option given by Android that the service will restart itself if the service is terminated.
Remember the question difference between multiprocessing and multithreading? Service is a background process (Just like activity without UI), The same way how you launch thread in the activity to avoid load on the MainThread (Activity/UI thread), the same way you need to launch threads on service to avoid load on service.
In a single statement, if you want a run a background continues task, you need to launch a StickyService and run the thread in the service on event base
Upvotes: 51
Reputation: 2176
Today I was looking for this and Mr Brandon Rude gave an excellent answer. Unfortunately, AsyncTask
is now deprecated. You can still use it, but it gives you a warning, which is very annoying. So an alternative is to use Executors
like this (in kotlin
):
Executors.newSingleThreadExecutor().execute(Runnable {
// todo: do your background tasks
runOnUiThread{
// update ui if you are in an activity
}
/*
requireActivity().runOnUiThread {
// update views / ui if you are in a fragment
}
*/
})
And in java
it looks like this:
Executors.newSingleThreadExecutor().execute(() -> {
// todo: background tasks
runOnUiThread(() -> {
// todo: update your ui / view in activity
});
/*
requireActivity().runOnUiThread((Runnable) () -> {
// todo: update your ui / view in Fragment
});
*/
});
Upvotes: 26
Reputation: 133560
An Alternative to AsyncTask is robospice. https://github.com/octo-online/robospice.
Some of the features of robospice.
1.executes asynchronously (in a background AndroidService) network requests (ex: REST requests using Spring Android).notify you app, on the UI thread, when result is ready.
2.is strongly typed ! You make your requests using POJOs and you get POJOs as request results.
3.enforce no constraints neither on POJOs used for requests nor on Activity classes you use in your projects.
4.caches results (in Json with both Jackson and Gson, or Xml, or flat text files, or binary files, even using ORM Lite).
5.notifies your activities (or any other context) of the result of the network request if and only if they are still alive
6.no memory leak at all, like Android Loaders, unlike Android AsyncTasks notifies your activities on their UI Thread.
7.uses a simple but robust exception handling model.
Samples to start with. https://github.com/octo-online/RoboSpice-samples.
A sample of robospice at https://play.google.com/store/apps/details?id=com.octo.android.robospice.motivations&feature=search_result.
Update: The above is answer is not valid anymore. Use kotlin coroutines for background threading.
Upvotes: 2
Reputation: 47481
A simple way of doing this that I found as a comment by @awardak in Brandon Rude's answer:
new Thread( new Runnable() { @Override public void run() {
// Run whatever background code you want here.
} } ).start();
I'm not sure if, or how, this is better than using AsyncTask.execute
but it seems to work for us. Any comments as to the difference would be appreciated.
Thanks, @awardak!
Upvotes: 39
Reputation: 3098
If you need run thread predioticly with different codes here is example:
Listener:
public interface ESLThreadListener {
public List onBackground();
public void onPostExecute(List list);
}
Thread Class
public class ESLThread extends AsyncTask<Void, Void, List> {
private ESLThreadListener mListener;
public ESLThread() {
if(mListener != null){
mListener.onBackground();
}
}
@Override
protected List doInBackground(Void... params) {
if(mListener != null){
List list = mListener.onBackground();
return list;
}
return null;
}
@Override
protected void onPostExecute(List t) {
if(mListener != null){
if ( t != null) {
mListener.onPostExecute(t);
}
}
}
public void setListener(ESLThreadListener mListener){
this.mListener = mListener;
}
}
Run different codes:
ESLThread thread = new ESLThread();
thread.setListener(new ESLThreadListener() {
@Override
public List onBackground() {
List<EntityShoppingListItems> data = RoomDB.getDatabase(context).sliDAO().getSL(fId);
return data;
}
@Override
public void onPostExecute(List t) {
List<EntityShoppingListItems> data = (List<EntityShoppingListItems>)t;
adapter.setList(data);
}
});
thread.execute();
Upvotes: 1
Reputation: 49
class Background implements Runnable {
private CountDownLatch latch = new CountDownLatch(1);
private Handler handler;
Background() {
Thread thread = new Thread(this);
thread.start();
try {
latch.await();
} catch (InterruptedException e) {
/// e.printStackTrace();
}
}
@Override
public void run() {
Looper.prepare();
handler = new Handler();
latch.countDown();
Looper.loop();
}
public Handler getHandler() {
return handler;
}
}
Upvotes: 2
Reputation: 4389
IF you need to:
execute code on a background Thread
execute code that DOES NOT touch/update the UI
execute (short) code which will take at most a few seconds to complete
THEN use the following clean and efficient pattern which uses AsyncTask:
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//TODO your background code
}
});
Upvotes: 431
Reputation: 33505
I want some code to run in the background continuously. I don't want to do it in a service. Is there any other way possible?
Most likely mechanizm that you are looking for is AsyncTask
. It directly designated for performing background process on background Thread. Also its main benefit is that offers a few methods which run on Main(UI) Thread and make possible to update your UI if you want to annouce user about some progress in task or update UI with data retrieved from background process.
If you don't know how to start here is nice tutorial:
Note: Also there is possibility to use IntentService
with ResultReceiver
that works as well.
Upvotes: 24