Usually I start an activity with this code:
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
But how do I start the activity so that is left in the background?
Usually I start an activity with this code:
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
But how do I start the activity so that is left in the background?
To keep an activity running in background , one can use services. Create a Background Service like :
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class BackgroundService extends Service {
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
BackgroundService getService() {
return BackgroundService.this;
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
Call the service in the oncreate() of your Main activity like this -
startService(new Intent( MainActivity.this,BackgroundService.class));
U can do three thing
If u want to perform long running task in background with UI update. Use Asyntask. If u want to perform long running task in background only use intentservice. If u want some background task which is not too heavy a little work use services.
Activity is usually meant to be shown to the user. If you do not need any UI you perhaps do not need to subclass Activity at all. Consider using i.e. Service or IntentService for your task. Or you can set Activity's theme to .NoDisplay
.