简体   繁体   English

后台服务用于在后台更新位置

[英]Background Service for update location in background

it kills background service, to solve your issue you should use foreground service.它会杀死后台服务,要解决您的问题,您应该使用前台服务。

my background sticky service is kill on oreo and heigher devices any solution for getting location on background service when Activity is on backound我的后台粘性服务在奥利奥和更高的设备上被杀死 任何用于在 Activity 处于后台时获取后台服务位置的解决方案

You would not be able to run background services long running in Oreo as there are behavior changes, now Oreo to optimize system memory, battery etc, it kills background service, to solve your issue you should use foreground service.由于行为发生变化,您将无法在 Oreo 中长时间运行后台服务,现在 Oreo 可以优化系统内存、电池等,它会终止后台服务,为了解决您的问题,您应该使用前台服务。

Have a look at Background execution limits https://developer.android.com/about/versions/oreo/android-8.0-changes看看后台执行限制https://developer.android.com/about/versions/oreo/android-8.0-changes

A suggestion from me, if you can use FCM then go for it, because apps like WeChat, Facebook uses it, to deliver notifications and they don't face any problem.我的一个建议,如果你可以使用 FCM,那就去吧,因为像微信、Facebook 这样的应用程序使用它来发送通知,他们不会遇到任何问题。

Alternate solution which I have opted without FCM due to client requirement to run service that updates location in background.由于客户端要求运行在后台更新位置的服务,我选择了不使用 FCM 的替代解决方案。 Here are steps below:-以下是以下步骤:-

  • You need to start your background service with showing notification in Oreo and above.您需要在 Oreo 及更高版本中显示通知来启动后台服务。
  • Them after that you need to keep in mind that after some time phone enters into Doze mode so you have to tackle that also之后你需要记住,一段时间后手机进入打盹模式,所以你也必须解决这个问题
  • In Addition, Battery optimization must be disabled for the application too.此外,还必须为应用程序禁用电池优化。
  • In Some custom ROM you need to manage the Auto-start permission to restart your service if the service is killed by the android.在某些自定义 ROM 中,如果服务被 android 杀死,您需要管理自动启动权限以重新启动您的服务。
  • And the most important part is, if the service is killed by the android system then send a Broadcast Message to the Broadcast receiver to restart your service one again最重要的部分是,如果服务被android系统杀死,则向广播接收器发送广播消息以再次重新启动您的服务

Hope you will do some more R&D work.希望你们多做一些研发工作。 I have shared my experience and the process by which i have done the same to run the service in the background.我分享了我的经验和我在后台运行服务的过程。

It's because of Android Oreo behaviour changes for background execution.这是因为 Android Oreo 的后台执行行为发生了变化。 Suggested alternatives are建议的替代方案是

1) Foreground service. 1)前台服务。

Use this if you are ok displaying notification to the user when you are trying to retrieve the location using Location API.如果您在尝试使用 Location API 检索位置时可以向用户显示通知,请使用此选项。 This would be reliable and is suggested in the documentation.这将是可靠的,并在文档中建议。

Sample app from the documentation : LocationUpdatesForegroundService project on GitHub文档中的示例应用程序:GitHub 上的LocationUpdatesForegroundService项目

An example of an app that allows the app to continue a user-initiated action without requesting all-the-time access to background location.一个应用程序示例,该应用程序允许应用程序继续执行用户启动的操作,而无需请求始终访问后台位置。

References
https://developer.android.com/training/location/receive-location-updates

2) Work Manager 2) 工作经理

This approach would be less reliable as you will not be able to control when exactly this would be called but can be used if you don't want to show notification to the user at all.这种方法不太可靠,因为您将无法控制何时确切地调用它,但是如果您根本不想向用户显示通知,则可以使用它。

you must show notification for ForegroundService您必须显示 ForegroundService 的通知

    public class ForegroundService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_NOT_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

and add permission并添加权限

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM