How can I run code on a background thread on Android?

AndroidMultithreading

Android Problem Overview


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

	}

Android Solutions


Solution 1 - Android

IF you need to:

  1. execute code on a background Thread

  2. execute code that DOES NOT touch/update the UI

  3. 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
   }
});

Solution 2 - Android

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

Solution 3 - Android

Simple 3-Liner

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!

Solution 4 - Android

> 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.

Solution 5 - Android

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
            });
            */
        });

Solution 6 - Android

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.

Solution 7 - Android

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;
    }
}

Solution 8 - Android

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();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2082987View Question on Stackoverflow
Solution 1 - AndroidBrandon RudeView Answer on Stackoverflow
Solution 2 - AndroidSandeep PView Answer on Stackoverflow
Solution 3 - AndroidJoshua PinterView Answer on Stackoverflow
Solution 4 - AndroidSimon DorociakView Answer on Stackoverflow
Solution 5 - AndroidQazi Fahim FarhanView Answer on Stackoverflow
Solution 6 - AndroidRaghunandanView Answer on Stackoverflow
Solution 7 - AndroidGeno PapashviliView Answer on Stackoverflow
Solution 8 - AndroidUcdemirView Answer on Stackoverflow