Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:11:57+00:00 2026-06-14T22:11:57+00:00

Basically, do I have to put code I want to run on another thread

  • 0

Basically, do I have to put code I want to run on another thread inside doInBackground, or can I call another function/class/whatever-it-is-functions-are-called-in-JAVA within doInBackground and have it run asynchronously? IE: (example code I found online)

      protected String doInBackground(String... params) {
        for(int i=0;i<5;i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        TextView txt = (TextView) findViewById(R.id.output);
        txt.setText("Executed");
        return null;
  }  

is how I have seen it done, but can I instead do:

      protected String doInBackground(String... params) {
           postToServer(x,y,z,h);
      }  

and have it call a function I already wrote and then have that function run in another thread? Sometimes my HTTP server is a bit slow to respond (it is but a lowly testing server at the moment) and Android automatically pops up the kill process box if my postToServer() call takes more than 5 seconds, and also disables my UI until the postToServer() call finishes. This is a problem because I am developing a GPS tracking app (internally for the company I work for) and the UI option to shut the tracking off freezes until my postToServer() finishes, which sometimes doesn’t ever happen. I apologize if this has been answered, I tried searching but haven’t found any examples that work the way I’m hoping to make this work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:11:58+00:00Added an answer on June 14, 2026 at 10:11 pm

    You can do that, but you will have to move the UI updates to onPostExecute as it is run on the UI thread.

    public MyAsyncTask extends AsyncTask<foo, bar, baz> {
    
        ...
    
        protected String doInBackground(String... params) {
           postToServer(x,y,z,h);
        }  
    
        protected void onPostExecute(Long result) {
            TextView txt = (TextView) findViewById(R.id.output);
            txt.setText("Executed");
        }
    
        ....
    
    }
    

    You may want to pass in the TextView to the constructor of the AsyncTask and store it as a WeakReference.
    private final WeakReference textViewReference;

    public MyAsyncTask(TextView txt) {
        textViewReference = new WeakReference<TextView>(txt);
    }
    

    And then in onPostExecute you would make sure that the TextView reference still exists.

    protected void onPostExecute(Long result) {
        TextView txt = textViewReference.get();
        if (txt != null)
            txt.setText("Executed");
    }
    

    If you want to notify the user that the task is executing I would put that before invoking the AsyncTask.

     myTextView.setText("Update in progress...");
     new MyAsyncTask().execute();
    

    then in onPostExecute set the TextView to say “Update complete.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have code which looks like this inside a header file: class Bar;
Basically i have a problem with this timer program I am trying to put
I have a double polymorphic associations, basically i'm going to put in relation Questions
I basically have a program that filters records from one excel file to another
what I mean by that is: I basically have a class that has too
I'm basically trying to teach myself how to code and I want to follow
I have the following jQuery code: $('#picture').live('change', function() { $(#formupload).ajaxForm({ target: '#newpic' }).submit(); });
So basically I have data being pulled from a database for which I want
I have some code that essentially does what I want but I always like
I'm trying to understand the asset_pipeline. I basically have all my website-wide code in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.