How to run an android application in the background?

This code starts the application automatically after loading the system, but the application closes after clicking the "Back" button.

If the application starts normally, by clicking the icon. It will work even after clicking the "Back" button or starting other applications.

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 run continuously even after clicking the back button or starting other applications?

+26
android android-service
Jan 27 2018-12-12T00:
source share
3 answers

Perhaps you can run Service here if you want your application to run in the background. This is what the Service in Android is used for - runs in the background and performs lengthy operations.

UDPATE

You can use START_STICKY to keep 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; } 
+16
Jan 27 '12 at 4:52
source share

Running an action is the wrong approach for this behavior. Instead, your BroadcastReceiver uses the intention of starting a service that can continue to run for as long as possible. (See http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle )

See also Continuous Service

+4
Jan 27 '12 at 4:53
source share

Since applications still work in the background. I assume that you are really asking how you are doing applications in the background. The solution below will make your application run in the background after opening the application and after rebooting the system.

Below Ive added a link to a fully working example (in the form of an Android Studio project).

This question seems to go beyond the scope of Android docs, and there seems to be no comprehensive document for this. The information applies to several documents.

The following documents indirectly indicate 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 order to properly use your usage requirements, an important part of this above document is carefully read: #Binder, #Messenger and components:

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

Here is a link to a fully working example (in Android Studio format): http://developersfound.com/BackgroundServiceDemo.zip

This project will start an action that is associated with the service; implementations of AIDL.

This project is also useful for redefinition for IPC purposes in different applications.

This project is also designed to automatically start when you restart Android (provided that the application was launched at least one after installation, and the application is not installed on the SD card).

When this application / project starts after a reboot, it dynamically uses a transparent view so that it looks as if no application was starting, but the linked application service starts cleanly.

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

This project was developed in accordance with the above documents and subsequently is a clean solution.

However, there is part of this project that is not clean: I did not find a way to start the service on reboot without using Activity. If any of you guys reading this post have a clean way to do this, write a comment.

+3
Apr 22 '17 at 3:56 on
source share



All Articles