GPS追踪应用程序(策略)

发布于 2025-01-07 04:31:19 字数 411 浏览 2 评论 0原文

我目前正在为汽车驾驶员日志开发 GPS 跟踪应用程序(仅距离,不存储每个值或在 MapView 中显示)。 因为有对接电话,我不关心功耗。 到目前为止,我的实现是一个调用 GPS-Helper 类的活动,该类获取经度/纬度。 该活动本身计算行驶距离,将其显示给用户,并启动一个也显示距离的通知栏。为了保持活动处于活动状态并且不被操作系统杀死,我对该活动使用了 PARTIAL WakeLock。

我的问题是,这无法正常工作,因为尽管有 WakeLock,我的应用程序似乎还是被操作系统杀死了。我认为它已被杀死,因为当我单击通知栏项目(例如 15-30 分钟后)查看跑步活动中的行驶距离时,该活动显示为启动新的 GPS 轨迹而不是显示距前一个开始轨迹的行驶距离。 WAKELOCK 权限在清单中正确设置。

我现在的问题是了解这个结构是否可以工作或者是否有更好的方法来做到这一点?

I am currently working on a GPS tracking App (only the distance, not storing each value or displaying in a MapView) for a car-drivers logbook.
Cause of a docked phone, I do not care about power consumption.
My implementation so far is an activity that calls a GPS-Helper class which is getting the long/lat.
The activity itself calculates the driven distance, displays it for the user and starts a notification bar that also displays the distance. To keep the activity active and not killed by the OS, I am using a PARTIAL WakeLock for the activity.

My problem is that this is not working correctly, cause my App seems to be killed by the OS inspite of the WakeLock. I think that it is killed, cause when I click on the notification bar item (after 15-30 min. for example) to see the driven distance in my running activity, the activity is shown as it is to start a new GPS-track instead of displaying the driven distance from the former started track.
The WAKELOCK Permission is correctly set in the Manifest.

My question now is to get know if this costruct could be working or is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昇り龍 2025-01-14 04:31:20

您的问题可能出在您单击通知时启动的 Intent。这种意图很可能认为您想要启动一个全新的 Activity,而不是将旧的 Activity 返回到前台。

此链接可能会帮助您完成您想要的操作:

如何通过通知将 Android 现有活动置于前台

Your problem may be with the Intent you are launching when you click on the notification. This intent is most likely thinking that you want to launch a brand new Activity rather than returning the old activity to the foreground.

This link may help you to do what you want:

How to bring Android existing activity to front via notification

等待圉鍢 2025-01-14 04:31:20

您应该使用调用 startForground 的服务,这需要通知。此通知将成为您返回应用程序的入口点。该服务可以在后台运行并记录坐标,而不依赖于您的 Activity 的生命周期。

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

            if(intent.getAction().equals(DRIVELOG_ACTION_STOPLOGGING)){               handleStopLoggingCommand(intent.getBooleanExtra(SAVE_LOG,false));
            }
            else if(intent.getAction().equals(DRIVELOG_ACTION_STARTLOGGING)){
                handleStartLoggingCommand();
            }
            return START_STICKY;
        }


   private void handleStartLoggingCommand() {
        startForeground(DriveLoggerNotification.notificationId,DriveLoggerNotification.createInDriveLogNotification(this,driveLogLiveData));
    if(googleApiClient.isConnected()){
        startMonitoringLocation();
    }else {
        googleApiClient.connect();
    }

}

此代码来自我的 GpsLoggingService

You should use a service which calls startForground, which requires a notification. This notification will be your entry point back into the app. The service can run in the background and log coordinates without depending on the life cycle of your Activity.

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

            if(intent.getAction().equals(DRIVELOG_ACTION_STOPLOGGING)){               handleStopLoggingCommand(intent.getBooleanExtra(SAVE_LOG,false));
            }
            else if(intent.getAction().equals(DRIVELOG_ACTION_STARTLOGGING)){
                handleStartLoggingCommand();
            }
            return START_STICKY;
        }


   private void handleStartLoggingCommand() {
        startForeground(DriveLoggerNotification.notificationId,DriveLoggerNotification.createInDriveLogNotification(this,driveLogLiveData));
    if(googleApiClient.isConnected()){
        startMonitoringLocation();
    }else {
        googleApiClient.connect();
    }

}

This code is from my GpsLoggingService

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文