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 7852337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:23:44+00:00 2026-06-02T19:23:44+00:00

I have an update database operation, that has an activity, which keeps updating the

  • 0

I have an update database operation, that has an activity, which keeps updating the percentage and it runs inside an AsyncTask.

Inside doInBackground() I call the controller that updates the database and keep updating the percentage on the activity, however, if I press home button or back button, the operation is cancelled. What u suggest me to do?

I was trying to start a Service inside doInBackground() so it would run in background, but it looks like its not working.

My code looks like this:

public class UpdateDatabaseAsyncTask extends AsyncTask<Void, Integer, Integer>
{   
    @Override
    public void onPreExecute()
    {
        mCustomProgressBar.startAnimation();
    }

    @Override
    public Integer doInBackground(Void... params)
    {
        return mController.updateDatabase();
    }

    @Override
    public void onPostExecute(Integer result)
    {
        mCustomProgressBar.stopAnimation();
        // finish the activity
    }

    @Override
    public void onProgressUpdate(Integer... value)
    {
        updatePercentageValue(value[0]);
    }

    public void callPublishProgress(Integer value)
    {
        publishProgress(value);
    }
}

And inside the controller I call the method callPublishProgress(value) passing the current percentage value, so it will publishProgress(value) in the UI.

I was debugging, and I pressed the home/back button, and it just stopped running the worker thread.


Another solution I tried, was starting a Service to run in background no matter the user press home/back button or not, so I thought, and the Service would make a call to the controller method that does the work, and it would call the callPublishProgress(value) to update the percentage value on the UI anyways.

However, what was happening is, the code reach doInBackground() and start the service, but it goes to onPostExecute() immediately, it just didn’t wait the service to finish(of course!). So it gives a NullPointerException. I thought of making a loop inside doInBackground() with a flag set in the Service, so it would leave this loop while the service hasn’t been finished (I was using an IntentService), but it didn’t work anyways.

I thought of using a Timer too. But I don’t know.

I was reading the articles in documentation about Threads, etc. And it suggests using AsyncTask, just as I was trying to do. It also talks about runOnUiThread(Runnable).

Anyways what I need is to make an operation in background(probably using an IntentService), so no matter if the user press the home button, it will keep running, but it must update the percentage on the UI, and when the user leave the screen and back to it, it shows the current percentage value updated in the screen.

What is the best solution for my case?

Thanks.

  • 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-02T19:23:45+00:00Added an answer on June 2, 2026 at 7:23 pm
    public class MyServce extends Service{
    public static final String BROADCAST_ACTION = "com.myapp";
    Intent intent;
    private final Handler handler = new Handler();
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        intent = new Intent(BROADCAST_ACTION);
    }
    
     @Override
        public void onStart(Intent intent, int startId) {
            handler.removeCallbacks(sendUpdatesToUI);
            handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
    
        }
       private Runnable sendUpdatesToUI = new Runnable() {
            public void run() {
                DoYourWorking();        
                handler.postDelayed(this, 1000); // 1 seconds
            }
    
            private void DoYourWorking() {
            ........
                        ........
                         intent.putExtra("key", progress);
                 sendBroadcast(intent);
    
            }
        };   
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onDestroy() {   
        super.onDestroy();
        handler.removeCallbacks(sendUpdatesToUI);       
    
    }
    

    Now in your Activity register broadcast to service

    private BroadcastReceiver brodcast = new BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, Intent intent) {
                     //intent.getWhatever
            // update your progress
            //progressbar.setProgress 
        }
    

    register broadcast

     registerReceiver(brodcast, new IntentFilter(MyService.BROADCAST_ACTION));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that keeps record history. For each update to a record,
I have some code to update a database table that looks like try {
I have a database entry that I need to update. This method obviously doesn't
I have a method that attempts to update a sql server database in an
I have an App in my Device, that contains database, if I update that
I'm a PHP noob. I have a database that I will update weekly with
I have a MySQL database table that has a VARCHAR field storing a date
I currently have a problem attepting to update a record within my database. I
I have a large database which will only be updated once a year. Every
I have an html/php form that updates entries on the database server. I need

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.