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?
Asked
Active
Viewed 950 times
1
-
put the Logs in all asynctask and services – Srikanth Jul 04 '13 at 10:00
-
I have active logs for all connections. But if I close the app, I don't get logs in Eclipse any more – iMx Jul 04 '13 at 10:10
3 Answers
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
-
-
@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
-
-
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