简体   繁体   English

每分钟发送位置的服务,即使应用程序在后台或系统重新启动

[英]Service to sending location every minute, even if app is in background or system is rebooted

I need to create an app to track the user location.我需要创建一个应用程序来跟踪用户位置。 The location must be sent to server every one minute (delays arent problems).位置必须每一分钟发送到服务器(延迟不是问题)。 But I'm having many troubles to set up a task/service that last forever (even if I close my app or reboot the system)但是我在设置一个永远持续的任务/服务时遇到了很多麻烦(即使我关闭了我的应用程序或重新启动系统)

*get the "last location" isn't the problem, the only problem is having a task/service, that runs "forever") *获取“最后一个位置”不是问题,唯一的问题是有一个“永远”运行的任务/服务)

I tried to use Service.我尝试使用服务。 But android, since Marshmallow(I guess) version, can kill background Apps and Services anytime(Its a feature to avoid many app creating background services/tasks and consuming too much battery and memory).但是android,从Marshmallow(我猜)版本开始,可以随时杀死后台应用程序和服务(这是一个避免许多应用程序创建后台服务/任务并消耗过多电池和内存的功能)。

Now I'm trying to use the new androidx "Worker" since i'd like to build an app with minSdk=16 and the current maxSdk(29).But now i'm facing two problems: - The Worker cant run for more than 10 minutes (so I cant create a Worker that last forever).现在我正在尝试使用新的 androidx“Worker”,因为我想用 minSdk=16 和当前的 maxSdk(29) 构建一个应用程序。但现在我面临两个问题: - Worker 不能运行更多超过 10 分钟(所以我不能创建一个永远持续的 Worker)。 - I could create a PeriodicWorkRequest, but the min time to re-schedule a Worker is 15 minutes (I need to send the location every one minute). - 我可以创建一个 PeriodicWorkRequest,但是重新安排一个 Worker 的最短时间是 15 分钟(我需要每隔一分钟发送一次位置)。

I tried to use OneTimeWorkRequest and re-scheduling itself, but its not works after a system reboot.我尝试使用 OneTimeWorkRequest 并重新安排自己,但在系统重新启动后它不起作用。

Any ideas/solutions?任何想法/解决方案?

First schedule in the main activity:主要活动中的第一个时间表:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        WorkManager.getInstance(this).enqueue(
            OneTimeWorkRequest.Builder(WorkerTest::class.java).build()
        )
    }
}

the Worker (re-scheduling itself at the init of "doWork", with the one minute delay): Worker(在“doWork”的初始化时重新调度自身,延迟一分钟):

public class WorkerTest(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {

    override fun doWork(): Result {
        WorkManager.getInstance(applicationContext).enqueue(
            OneTimeWorkRequest.Builder(this::class.java).setInitialDelay(1, TimeUnit.MINUTES).build()
        )

        //TODO get and send location

        return Result.success()
    }

}

Use AlarmManager and run every minute to send your request to the server.使用 AlarmManager 并每分钟运行一次以将您的请求发送到服务器。

https://developer.android.com/reference/android/app/AlarmManager https://developer.android.com/reference/android/app/AlarmManager

Well, I will tell you an idea:好吧,我告诉你一个想法:

1.Run your service on foreground/background: You can use a foreground service, this service is intended to run always, it is attached to a notification bar and if the user dismisses the notification the service will die, android will never let you run a service forever if the user is not aware of it. 1.在前台/后台运行您的服务:您可以使用前台服务,该服务旨在始终运行,它附加到通知栏,如果用户关闭通知,该服务将死亡,android 永远不会让您运行如果用户不知道,则服务将永远存在。

2.Run your service after a Reboot: In order to run this service after a reboot, you will need to use a broadcast receiver for that system event. 2.在重启后运行您的服务:为了在重启后运行此服务,您需要为该系统事件使用广播接收器。 So when the reboot finishes you need to call your foreground service.因此,当重启完成时,您需要调用前台服务。

This is the road map that you have to follow.这是您必须遵循的路线图。 Remember that you will need user permissions for the location updates.请记住,您将需要位置更新的用户权限。 And Depending on the SDK version the code, methods, and special needs on each one will be different.并且根据 SDK 版本的不同,每一个的代码、方法和特殊需求都会有所不同。

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

相关问题 Android - 即使应用程序被终止或设备重新启动,也可以在后台运行服务 - Android - Run a service in background even when app is killed or device rebooted 使用后台服务每1分钟或每1公里更改获取当前位置 - Get current location for every 1 minute or every 1km change using background service 每15分钟启动一次服务并在android中发送位置信息后销毁它是一种好习惯吗? - Is it good Practice to start service every 15 minute & destroy it after sending location information in android? 如何每1分钟在后台运行一次服务 - How to run service in background every 1 minute 如何每10分钟在后台运行一次服务? - How to run service in background every 10 minute? 我想每 1 分钟将我的位置发送到服务器,即使是 android 后台的应用程序? - I want to send my location to the server each 1 minute even the app in the background in android? 应用每分钟在后台发送信息 - App sending information each minute in background 系统每1分钟启动和重复一次时如何启动服务 - How to start a service when system boot and repet every 1 minute 如何每分钟在Android O中运行后台服务? - How to run the background service in Android O, every minute? 使后台服务在启动时以及每分钟运行 - Make Background Service run on startup as well as every minute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM