views:

95

answers:

2

Hi,

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.

A: 

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".

CommonsWare
A: 

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.

Mark