26

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?

Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
JBMagallanes
  • 271
  • 1
  • 3
  • 4

3 Answers3

17

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;
}
Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
5

As apps run in the background anyway, I'm assuming what your really asking is how do you make apps do stuff in the background. The solution below will make your app do stuff in the background after opening the app and after the system has rebooted.

Below, I've added a link to a fully working example (in the form of an Android Studio Project).

This subject seems to be out of the scope of the Android docs, and there doesn't seem to be any one comprehensive doc on this. The information is spread across a few docs.

The following docs tell you indirectly how to do this:

https://developer.android.com/reference/android/app/Service.html

https://developer.android.com/reference/android/content/BroadcastReceiver.html

https://developer.android.com/guide/components/bound-services.html

In the interests of getting your usage requirements correct, the important part of this above doc to read carefully is: #Binder, #Messenger and the components link below:

https://developer.android.com/guide/components/aidl.html

Here is the link to a fully working example (in Android Studio format):

https://developersfound.com/BackgroundServiceDemo.zip

This project will start an Activity which binds to a service; implementing the AIDL.

This project is also useful to re-factor for the purpose of IPC across different apps.

This project is also developed to start automatically when Android restarts (provided the app has been run at least one after installation and app is not installed on SD card).

When this app/project runs after reboot, it dynamically uses a transparent view to make it look like no app has started but the service of the associated app starts cleanly.

This code is written in such a way that it's very easy to tweak to simulate a scheduled service.

This project is developed in accordance to the above docs, and is subsequently a clean solution.

There is, however, a part of this project which is not clean: I have not found a way to start a service on reboot without using an Activity. If anyone reading this post has a clean way to do this, please post a comment.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user2288580
  • 2,210
  • 23
  • 16
  • thanks - I am very interested in learning this. I have been developing Android Apps in Android Studio for almost 1 year. I downloaded your zip, put it in a directory, opened that director in Android Studio and upon build received this error. Error:A problem occurred configuring project ':app'. > Could not resolve all dependencies for configuration ':app:_debugApkCopy'. > Could not find com.android.support.constraint:constraint-layout:1.0.2. Required by: project :app – Frank Zappa Aug 17 '17 at 01:43
  • I'm guessing that one of your layouts is using constraint layout when the constraint layout dependency is not in your gradle file. If this is not the case change the constraint layout in you layout file to an ordinay layout and if there are any properties in the layout relating to constrain layout remove them also. – user2288580 Aug 23 '17 at 21:14
  • When new gradle versions come out there are normally migration issues. You're best way to deal with this problem is to make a new project with the same package name and migrate the project files over separately. Sorry about the inconvenience. The app was thoroughly tested so it does work. – user2288580 Jan 15 '18 at 03:13
4

Starting an Activity is not the right approach for this behavior. Instead have your BroadcastReceiver use an intent to start a Service which can continue to run as long as possible. (See http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle)

See also Persistent service

Community
  • 1
  • 1
Jonah
  • 17,918
  • 1
  • 43
  • 70