如何在后台运行Android应用程序?

时间:2020-12-10 01:25:58

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?

我的问题是,即使在按下后退按钮或运行其他应用程序后,如何使此自动运行代码继续运行?

3 个解决方案

#1


16  

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.

如果您希望应用程序在后台运行,则可以在此处启动服务。这就是Android中的服务用于 - 在后台运行并进行长时间操作。

UDPATE

You can use START_STICKY to make your Service running continuously.

您可以使用START_STICKY使您的服务持续运行。

@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;
}

#2


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)

启动活动不是此行为的正确方法。而是让您的BroadcastReceiver使用意图启动服务,该服务可以继续尽可能长时间运行。 (参见http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle)

See also Persistent service

另请参见持久性服务

#3


4  

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)

下面,我添加了一个完整工作示例的链接(以Android Studio项目的形式)

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.

这个主题似乎超出了Android文档的范围,似乎没有任何一个综合性的文档。这些信息分散在几个文档中。

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/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:

为了使您的使用要求正确,请仔细阅读以上文档的重要部分:#Binder,#Messenger和下面的组件链接:

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

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

以下是完整工作示例的链接(采用Android Studio格式):http://developersfound.com/BackgroundServiceDemo.zip

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

该项目将启动一个绑定到服务的Activity;实施AIDL。

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

该项目对于在不同应用程序中重新考虑IPC的目的也很有用。

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)

此项目也被开发为在Android重启时自动启动(假设应用程序在安装后至少运行一次且SD卡上未安装应用程序)

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 being: I have not found a way to start a service on reboot without using an Activity. If any of you guys reading this post have a clean way to do this please post a comment.

然而,这个项目的一部分并不干净:我没有找到一种方法在重启时启动服务而不使用Activity。如果您有任何人阅读这篇文章有一个干净的方式来做到这一点,请发表评论。

#1


16  

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.

如果您希望应用程序在后台运行,则可以在此处启动服务。这就是Android中的服务用于 - 在后台运行并进行长时间操作。

UDPATE

You can use START_STICKY to make your Service running continuously.

您可以使用START_STICKY使您的服务持续运行。

@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;
}

#2


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)

启动活动不是此行为的正确方法。而是让您的BroadcastReceiver使用意图启动服务,该服务可以继续尽可能长时间运行。 (参见http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle)

See also Persistent service

另请参见持久性服务

#3


4  

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)

下面,我添加了一个完整工作示例的链接(以Android Studio项目的形式)

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.

这个主题似乎超出了Android文档的范围,似乎没有任何一个综合性的文档。这些信息分散在几个文档中。

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/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:

为了使您的使用要求正确,请仔细阅读以上文档的重要部分:#Binder,#Messenger和下面的组件链接:

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

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

以下是完整工作示例的链接(采用Android Studio格式):http://developersfound.com/BackgroundServiceDemo.zip

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

该项目将启动一个绑定到服务的Activity;实施AIDL。

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

该项目对于在不同应用程序中重新考虑IPC的目的也很有用。

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)

此项目也被开发为在Android重启时自动启动(假设应用程序在安装后至少运行一次且SD卡上未安装应用程序)

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 being: I have not found a way to start a service on reboot without using an Activity. If any of you guys reading this post have a clean way to do this please post a comment.

然而,这个项目的一部分并不干净:我没有找到一种方法在重启时启动服务而不使用Activity。如果您有任何人阅读这篇文章有一个干净的方式来做到这一点,请发表评论。