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

问题描述 投票:0回答:4

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

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

android
4个回答
119
投票

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

创建服务:

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

创建一个应用程序类并启动您的服务:

public class App extends Application {

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

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

将“name”属性添加到 AndroidManifest.xml 的“application”标签中

android:name=".App"

另外,不要忘记在 AndroidManifest.xml 的“application”标签中添加您的服务

<service android:name=".YourService"/>

还有“manifest”标签中的此权限请求(如果 API 级别为 28 或更高):

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

更新

Android Oreo 之后,Google 引入了一些后台限制。因此,上面的解决方案可能行不通。当用户从任务管理器终止您的应用程序时,Android 系统也会终止您的服务。如果您想运行一个始终在后台运行的服务。您必须运行前台服务并显示正在进行的通知。因此,如下编辑您的服务。

public class YourService extends Service {

    private static final int NOTIF_ID = 1;
    private static final String NOTIF_CHANNEL_ID = "Channel_Id";

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){

        // do your jobs here

        startForeground();
        
        return super.onStartCommand(intent, flags, startId);
    }

    private void startForeground() {
        Intent notificationIntent = new Intent(this, MainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        startForeground(NOTIF_ID, new NotificationCompat.Builder(this, 
                NOTIF_CHANNEL_ID) // don't forget create a notification channel first
                .setOngoing(true)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("Service is running background")
                .setContentIntent(pendingIntent)
                .build());         
    }
}

编辑:受限 OEM

不幸的是,一些 OEM(小米、OnePlus、三星、华为等)由于提供更长的电池寿命而限制后台操作。对于这些 OEM 厂商来说,还没有合适的解决方案。用户需要允许一些特定于 OEM 的特殊权限,或者他们需要通过设备设置将您的应用程序添加到白名单应用程序列表中。您可以从https://dontkillmyapp.com/找到更多详细信息。

如果后台操作是您的义务,您需要向用户解释为什么您的功能不起作用,以及他们如何通过允许这些权限来启用您的功能。我建议您使用 AutoStarter 库 (https://github.com/judemanutd/AutoStarter),以便从您的应用轻松重定向有关权限页面的用户。

顺便说一句,如果您需要运行一些定期工作而不是连续的后台作业。你最好看看 WorkManager (https://developer.android.com/topic/libraries/architecture/workmanager)


4
投票

在像我这样的手机(MIUI Redmi 3)上,您只需在列表中添加特定的应用程序,当您在任务管理器中终止应用程序时,应用程序不会停止(它将停止,但会再次启动)

只需转到“设置”>“权限自动启动”


0
投票

在 mi 和 vivo 中 - 使用上述解决方案是不够的。您还必须告诉用户手动添加权限。 您可以通过打开手机设置中的正确位置来帮助他们。不同手机型号有所不同。


0
投票

import android.app.Service import android.content.Intent import android.os.IBinder import androidx.annotation.Nullable class ServiceName : Service() { @Nullable override fun onBind(intent: Intent?): IBinder? { return null } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { // do your jobs here return super.onStartCommand(intent, flags, startId) } }

同时声明您的服务

<service android:name=".ServiceName"/>

启动您的服务(如果需要,请考虑必要的权限)

startService(Intent(this, ServiceName::class.java))

© www.soinside.com 2019 - 2024. All rights reserved.