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

新手上路,请多包涵

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

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

原文由 Manuel 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
1 个回答

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

创建服务:

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

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

 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 的“应用程序”标签中添加您的服务

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

还有这个“清单”标签中的权限请求(如果 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 )

原文由 sembozdemir 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题