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

时间:2023-02-10 01:26:11

I'm using an android device. When I open the Settings > Apps > Running, there is an option on the top-left of the screen to see : (1) Running processes (2) Cached background processes. And the app(s) which I wanted to run always (24×7) is/are unfortunately listed under (2) Cached background processes, which I wanted it/them to be listed under (1) Running processes (if it is possible). Can it be done? Or in short, how to make an android app to always run in background?

我正在使用Android设备。当我打开设置>应用程序>运行时,屏幕左上角有一个选项可以看到:(1)正在运行的进程(2)缓存的后台进程。我想要运行的应用程序(24×7)不幸地列在(2)缓存的后台进程中,我希望它/它们列在(1)运行进程下(如果可能的话) )。可以吗?或者简而言之,如何使Android应用程序始终在后台运行?

I hope I conveyed the question :-)

我希望我转达了这个问题:-)

2 个解决方案

#1


30  

You have to start a service in your Application class to run it always. If you do that, your service will be always running. Even though user terminates your app from task manager or force stop your app, it will start running again.

您必须在Application类中启动服务才能始终运行它。如果您这样做,您的服务将始终运行。即使用户从任务管理器终止您的应用程序或强制停止您的应用程序,它也会再次开始运行。

Create a service:

创建服务:

public class YourService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // do your jobs here
        return super.onStartCommand(intent, flags, startId);
    }
}

Create an Application class and start your service:

创建一个Application类并启动您的服务:

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, YourService.class));
    }
}

Don't forget add this in "application" tag of your AndroidManifest.xml

不要忘记在AndroidManifest.xml的“application”标签中添加它

android:name=".App"

#2


0  

On some mobiles like mine (MIUI Redmi 3) you can just add specific Application on list where application doesnt stop when you terminate applactions in Task Manager (It will stop but it will start again)

在我的一些手机上(MIUI Redmi 3)你可以在列表中添加特定的应用程序,当你在任务管理器中终止应用程序时应用程序不会停止(它会停止但它会重新开始)

Just go to Settings>PermissionsAutostart

只需转到设置>权限自动启动即可

#1


30  

You have to start a service in your Application class to run it always. If you do that, your service will be always running. Even though user terminates your app from task manager or force stop your app, it will start running again.

您必须在Application类中启动服务才能始终运行它。如果您这样做,您的服务将始终运行。即使用户从任务管理器终止您的应用程序或强制停止您的应用程序,它也会再次开始运行。

Create a service:

创建服务:

public class YourService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // do your jobs here
        return super.onStartCommand(intent, flags, startId);
    }
}

Create an Application class and start your service:

创建一个Application类并启动您的服务:

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, YourService.class));
    }
}

Don't forget add this in "application" tag of your AndroidManifest.xml

不要忘记在AndroidManifest.xml的“application”标签中添加它

android:name=".App"

#2


0  

On some mobiles like mine (MIUI Redmi 3) you can just add specific Application on list where application doesnt stop when you terminate applactions in Task Manager (It will stop but it will start again)

在我的一些手机上(MIUI Redmi 3)你可以在列表中添加特定的应用程序,当你在任务管理器中终止应用程序时应用程序不会停止(它会停止但它会重新开始)

Just go to Settings>PermissionsAutostart

只需转到设置>权限自动启动即可