Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run an android app in background?

This code will run an app automatically after booting the system, but the app will close after pressing the back button.

If the app is run normally by clicking it's icon. It will continuously run even after pressing the back button or running other apps.

public class AutoBoot extends BroadcastReceiver {
    @Override        
    public void onReceive(Context context, Intent intent) {                
        Intent i = new Intent(context, MyActivity.class); 
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);          
    }
}

My question is, how to make this auto run code to continuously run even after pressing the back button or running other apps?

like image 333
JBMagallanes Avatar asked Sep 10 '25 19:09

JBMagallanes


1 Answers

You can probably start a Service here if you want your Application to run in Background. This is what Service in Android are used for - running in background and doing longtime operations.

UDPATE

You can use START_STICKY to make your Service running continuously.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    handleCommand(intent);
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}
like image 135
Lalit Poptani Avatar answered Sep 12 '25 09:09

Lalit Poptani