1

I have an Android app that should stay closed if it lose focus. But if the app is closed it still sends background data. How can I prevent the app from sending it or find out which async threads are still active?

iMx
  • 846
  • 1
  • 9
  • 23

3 Answers3

2

try this:-

Suppose you have one AsyncTask like:-

class BackgroundFetching extends AsyncTask<Void, Void, Integer>
    {

    }

make its object like

private BackgroundFetching mFetching ;

now when app is closing, cancel this AsyncTask like:-

@Override
protected void onPause() {
    super.onPause();
        if(null != mFetching && mFetching.getStatus() == AsyncTask.Status.RUNNING)
        {
            mFetching.cancel(true);
            mFetching = null;
        }
}
Sulabh Gupta
  • 642
  • 1
  • 7
  • 20
0

Detect if the application goes to backgound described here

Than send / set a flag to your classes to stop they activity of sending data.

Community
  • 1
  • 1
  • I do it this way: onUserLeaveHint(): activity.finish(); int pid = android.os.Process.myPid(); android.os.Process.killProcess(pid); Should I separately close all other activities? – iMx Jul 04 '13 at 10:11
  • not enough to kill the process programamtically :) some services aren't killed. kill they logic :) –  Jul 04 '13 at 10:13
  • So I have to log all started services and kill them on app stop? – iMx Jul 04 '13 at 10:35
  • @iMx I would do like this: step 1 detect the app when enters in background. Step2. Set a boolean flag to all of your running async tasks (need to keep reference of they ofc. Step 3: in the async task if the flag is set, than stop the task, do not run anymore. Nothing to do with logcat or process kill –  Jul 04 '13 at 10:47
  • for what should stay the boolean flags? – iMx Jul 08 '13 at 12:31
  • @iMx while(canRun){processAChunkOfData(data);} something like that –  Jul 08 '13 at 12:40
0

Kill the application when it lose focus, in other term onPause

@Override
protected void onPause() 
{
    super.onPause();
    finish();
}//End of onPause method
Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45