将获取的位置设置为文本视图:如何维护?

发布于 2024-09-01 00:04:59 字数 566 浏览 6 评论 0原文

我为摩托罗拉 Droid 构建了一个应用程序,它应该自动更新服务器的手机位置。用户在主活动屏幕上执行特定任务后,将设置警报以使用服务定期更新用户的位置。当用户完成另一项任务时,警报会明确停止。

事实是,我在主活动的 onCreate() 方法中设置了一个位置管理器,该方法应该将第一个获取的纬度/经度放入两个文本视图字段中。即使清单设置为获取粗略和精细坐标,并且我使用 requestLocationUpdates (Stringprovider, long minTime, float minDistance, LocationListener Listener),并将 minTime 和 minDistance 设置为零,我'我没有看到屏幕上出现坐标。这样,我就不会在服务器上记录任何位置。

当我用示例坐标播种文本视图时,它们在服务器上记录得很好。我没有可以运行 IDE 的计算机,因此目前没有代码,但我迫切需要一些帮助。

另一件事是,在用户手动单击“发送数据”之前,主活动屏幕会调用摄影应用程序。我怀疑我可能需要重写主活动的 onResume() 方法来执行此位置获取。请帮忙,谢谢。

标记。

I have built an app for the Motorola Droid which should automatically update a server with the phone's location. After the user performs a particular task on the main activity screen, an alarm is set to update the user's location periodically, using a service. The alarm is explicitly stopped when the user completes another task.

Thing is, I have set up a location manager within the main activity's onCreate() method which is supposed to place the first acquired lat/long into two textview fields. Even though the manifest is set up for acquiring coarse and fine coords and I'm using requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener), with minTime and minDistance set to zero, I'm not seeing the coords coming up on the screen. With that, I'm not recording any locations on the server.

When I seed the textviews with sample coords, they are being recorded fine on the server. I am not at a computer that can run the IDE, so don't currently have the code, but am desperate for some help on this.

One other thing is that the main activity screen calls a photography app before the user manually clicks "send data". I'm suspicious that I may need to override the main activity's onResume() method to do this location acquisition. Please help, thanks.

Mark.

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

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

发布评论

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

评论(2

最美的太阳 2024-09-08 00:04:59

对于如此复杂的事情,如果没有代码,很难为您提供帮助。

完全在黑暗中尝试将 requestLocationUpdates()AlarmManager 警报和服务结合使用是很难做到的。例如,如果服务是 IntentService,并且它正在注册位置更新,那么在最好的情况下它只会失败,而在最坏的情况下您将泄漏内存,例如一个筛子。还有一个问题是,在等待位置修复输入时,请确保设备处于唤醒状态,因为设备可能倾向于返回睡眠状态。

我不知道 TextViews 在哪里发挥作用,或者它们与某些服务器有什么关系,或者它们与 onResume() 有什么关系,或者他们与“摄影应用程序”有什么关系。

For something this complex, without code, it is very difficult to help you.

Taking a total stab in the dark, attempting to use requestLocationUpdates() in conjunction with an AlarmManager alarm and a service is difficult to do. If the service is an IntentService, for example, and it is registering for the location updates, in the best-case scenario it'll simply fail, and in the worst-case scenario you will leak memory like a sieve. There is also the matter of making sure the device is awake while you are waiting for the location fix to come in, since the device may tend to want to go back to sleep.

I have no idea where the TextViews come into play for all of this, or what they have to do with some server, or what they have to do with onResume(), or what they have to do with "a photography app".

浅暮の光 2024-09-08 00:04:59

谢谢CommsWare。由于评论区空间有限,我就用这种方式来回复。

用户应该为他们即将取走的物品拍照并将其发送到服务器。完成此操作后,服务器会发回一个轮询周期,以便手机定期发送其位置。我已经设置了 textViews 来显示获取的纬度/经度(见下文),但这部分直到两天前才开始工作。当我更有耐心并让手机慢慢地获取位置后,它终于起作用了。我认为当时的问题是我不允许手机获得位置修复,所以它不起作用。现在的问题仍然是,根据您所说的,以及基于我在完全晴朗的天空下站了大约 2 分钟才获得位置修复的事实,定期更新可能不起作用。

下面是一些代码的快照(这部分位于主活动的 onCreate 重写中,以获取第一个位置修复):

`LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, new LocationListener() {
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    // called when the provider status changes. Possible
                    // status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or
                    // AVAILABLE.
                }

                public void onProviderEnabled(String provider) {
                    // called when the provider is enabled by the user
                }

                public void onProviderDisabled(String provider) {
                    // called when the provider is disabled by the user, if
                    // it's already disabled, it's called immediately after
                    // requestLocationUpdates
                }

                public void onLocationChanged(Location location) {
                    double latitute = location.getLatitude();
                    double longitude = location.getLongitude();
                    tv5.setText(""+latitute);
                    tv6.setText(""+longitude);
                }
            });

`

下面是 SOAP 请求和响应处理机制的一部分。
`//获取轮询周期(以秒为单位)。
String[] getPollNumber;
getPollNumber = resultParams[2].split(";");

            //Set up the intent to be passed to the tracking service.
            Intent updateLocation = new Intent (this,TrackUser.class);  
            PendingIntent pendingIntent = PendingIntent.getService(PickUp.this, 0, updateLocation, 0);

            //Set up the alarm manager to be used to periodically send updates.
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

            //If instruction = 1, start tracking the user.
            if(instruction == 1)
            {                   
                //Start tracking service on phone.
                pollPeriod = Integer.parseInt(getPollNumber[0]);
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (pollPeriod*1000), pollPeriod*1000, pendingIntent);
            }
            else
            {
                if(instruction == 0)
                {
                    //Stop tracking service.
                    stopService(new Intent(this,TrackUser.class));
                    alarmManager.cancel(pendingIntent);
                }
            }

'

在跟踪服务类中,我使用从其 onStart() 重写中调用的方法,该方法只是将当前坐标打包到 SOAP 对象中并更新服务器。我担心如果手机像你说的那样进入睡眠状态,GPS 可能没有足够的时间在这些更新期间锁定。我想知道当服务启动时,我是否应该有某种计时器(也许是 requestLocationUpdates 的 minTime 设置?)让手机保持唤醒状态大约 3 分钟,以便修复?请告诉我这些代码片段是否有帮助,以及更新位置的想法是否可行,谢谢。

Thanks CommsWare. Because of the space limitation in the comment field, I'm using this way to respond.

The users are supposed to take a photo of an item they are about to pickup and send it to the server. After this is done, the server sends back a polling cycle period for the phone to send its location periodically. I've set up the textViews to show the acquired lat/long (see below), but this part wasn't working until two nights ago. It did finally work after I was a little more patient and let the phone take its time to acquire a position. I think the problem then was that I wasn't allowing the phone to acuire a position fix, so it wasn't working. The problem remains now that the periodic updates may not work, based on what you have said, and based on the fact that I stood outside under completely clear sky for about 2 mins before I got a position fix.

Here's a snapshot of some of the code (this part is in the main activity's onCreate override to get the first location fix):

`LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, new LocationListener() {
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    // called when the provider status changes. Possible
                    // status: OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE or
                    // AVAILABLE.
                }

                public void onProviderEnabled(String provider) {
                    // called when the provider is enabled by the user
                }

                public void onProviderDisabled(String provider) {
                    // called when the provider is disabled by the user, if
                    // it's already disabled, it's called immediately after
                    // requestLocationUpdates
                }

                public void onLocationChanged(Location location) {
                    double latitute = location.getLatitude();
                    double longitude = location.getLongitude();
                    tv5.setText(""+latitute);
                    tv6.setText(""+longitude);
                }
            });

`

Below is part of the SOAP request and response handling mechanism.
`//Get the polling period in seconds.
String[] getPollNumber;
getPollNumber = resultParams[2].split(";");

            //Set up the intent to be passed to the tracking service.
            Intent updateLocation = new Intent (this,TrackUser.class);  
            PendingIntent pendingIntent = PendingIntent.getService(PickUp.this, 0, updateLocation, 0);

            //Set up the alarm manager to be used to periodically send updates.
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

            //If instruction = 1, start tracking the user.
            if(instruction == 1)
            {                   
                //Start tracking service on phone.
                pollPeriod = Integer.parseInt(getPollNumber[0]);
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (pollPeriod*1000), pollPeriod*1000, pendingIntent);
            }
            else
            {
                if(instruction == 0)
                {
                    //Stop tracking service.
                    stopService(new Intent(this,TrackUser.class));
                    alarmManager.cancel(pendingIntent);
                }
            }

'

In the tracking service class, I use a method called from its onStart() override that simply packages the current coordinates into a SOAP object and updates the server. I'm worried that the GPS may not have sufficient time to lock on during these updates if the phone goes to sleep like you said. I'm wondering if when the service is started I should have a timer of some sort (maybe the minTime setting of requestLocationUpdates?) hold the phone awake for about 3 minutes so a fix could come in? Please let me know if these code snippets help, and if the idea for updating the position is possible, thanks.

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