How to Execute Background Task When Android App Is Closed/Set to Background

How to execute background task when Android app is closed / set to background?

The following code will help you to post your contents when your app is closed or in the background:

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;

public class SendDataService extends Service {
private final LocalBinder mBinder = new LocalBinder();
protected Handler handler;
protected Toast mToast;

public class LocalBinder extends Binder {
public SendDataService getService() {
return SendDataService .this;
}
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

@Override
public void onCreate() {
super.onCreate();

}

@Override
public void onDestroy() {
super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {

// write your code to post content on server
}
});
return android.app.Service.START_STICKY;
}

}

More explanation of my code is:

Service runs in the background even if your application is in the background, but remember, it always runs on the main thread, because of which I created a separate thread to perform your background operations.

Service is started as a sticky one, as even if because of any reason your service got destroyed in the background it will automatically get restarted.

More details can be found here:
https://developer.android.com/guide/components/services.html

And to check if your app is in the foreground/background, the following code will help:

  private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}

// Use like this:
boolean foregroud = new ForegroundCheckTask().execute(context).get();

Happy Coding!!!!

Run code when Android app is closed/sent to background

Edit

This answer only serves for one purpose, that is, running a code in onPause() for all activities. It doesn't let you run a code when your app is sent to background.

Original Answer

Make an Activity named YourBasicActivity and override its onPause() method and extend every Activity from YourBasicActivity

UWP in-process background task when app is closed

Turns out, contrary to the answer the condescending Microsoft employee provided, that the background task will run if the app is closed, even if it is implemented as an in-process background task.

If the background task is implemented as an in-process task, then the OnBackgroundActivated method will be used as the entry point, in case the app is not running. Otherwise, the method will be called while the app is running.

The reason why there were problems with the background task starting was that I used a condition (namely the SystemConditionType.UserPresent) on the task. This behaves very unpredictably. However, I tested (by waiting for several calls) both the in-process and out-of-process approach and the task gets run in both cases, even if the app is closed.

If a task deferral is used, the background task should not be terminated even if the user closes the app.

In conclusion, at the time of writing this post (October 17th, 2020) the documentation does not clearly explain this behavior and the aforementioned condescending misinterpretation by, ironically enough, Microsoft's own employee only proves that point.

How to Keep AsyncTask when user closes app

I want the AsyncTask to continue put data in my server in the background after the user closes the app.

The purpose of AsyncTask is NOT to perform background tasks that's beyond the scope of an Activity's lifecycle. When a background thread is assigned from a thread pool, it expects to return it once the task is over. ie you cannot use an AsyncTask for which the required time is indefinite.

What you are looking for is Services in Android .

Why even use AsyncTask if we have services?

Well, say for instance you require to download an image/song from an Activity on click of a Button or you need to perform a task that would take some time to finish its execution. Performing these tasks from the Main thread(aka UI thread) is a bad approach and would make your app sluggish and eventually can lead to an ANR. So these tasks are to be processed asynchronously from a separate thread to keep the app butter smooth.

How can i perform task even if the app is closed in React Native

This library implement's background tasks. It's implementation is in JS and pretty easy to use: https://github.com/Rapsssito/react-native-background-actions



Related Topics



Leave a reply



Submit