I posted this same question on StackOverflow if anyone wants the karma
for answering it there.

This is on an Android phone running 2.3.3 Gingerbread. I am trying to
log a GPS location while the screen is off. When the screen is on, the
GPS gives correct outputs... driving around in my car I have a phone
hooked up to my laptop and I read the output with logcat. Under the
same conditions, when I turn off the screen the GPS gives the same
location over and over, until I turn on my screen again at which time
it works properly.

Here is my manifest file (note the WAKE_LOCK and GPS permissions as
well as the LocationService definition):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
    package="com.joshuajwitter.backgroundgpstest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".BackgroundGPSTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".service.LocationService" >
            <intent-filter>
                <action
android:name="com.joshuajwitter.backgroundgpstest.service.LocationService" /
>
            </intent-filter>
        </service>
    </application>

</manifest>
Here is my background service:

package com.joshuajwitter.backgroundgpstest.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

    public class LocationService extends Service {

    private static final String TAG = "LocationService";

    // the location manager and listener
    LocationManager m_locationManager;
    GPSLocationListener m_gpsLocationListener;

    // the dreaded wakelock
    WakeLock m_wakeLock;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {

        Log.d(TAG, "Starting the LocationService");

        // acquire the wakelock
        Log.d(TAG, "Acquiring the wake lock");
        m_wakeLock.acquire();
    }

    @Override
    public void onCreate() {

        // get the wakelock
        PowerManager pm = (PowerManager)
getSystemService(Context.POWER_SERVICE);
        m_wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "String Wake Lock");

        // instantiate the gps listener
        m_gpsLocationListener = new GPSLocationListener();

        // get the location manager
        m_locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

        // request location updates every 5 seconds
 
m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                5000, 0, m_gpsLocationListener);
    }

    @Override
    public void onDestroy() {

        Log.d(TAG, "Stopping the LocationService");

        // if the wakelock is held
        if (m_wakeLock.isHeld()) {

            // release it
            Log.d(TAG, "Releasing the wake lock");
            m_wakeLock.release();
        }

        // remove the listener
        m_locationManager.removeUpdates(m_gpsLocationListener);
    }

    private class GPSLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            Log.d(TAG, "Got a new location frome the LocationManager
[" + location.getLatitude()
                    + ", " + location.getLongitude() + "]");
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status,
Bundle extras) {
        }
    }
}
..and here is the activity that starts and stops the service:

package com.joshuajwitter.backgroundgpstest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.joshuajwitter.backgroundgpstest.service.LocationService;

public class BackgroundGPSTestActivity extends Activity {

    private boolean m_serviceRunning = false;
    private Button m_mainButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get the main button
        m_mainButton = ((Button) findViewById(R.id.mainButton));

        // set the main button listener
        m_mainButton
        .setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {

                // if the service is not running
                if (!m_serviceRunning) {

                    // start the service
                    startService();
                }

                else {

                    // start the service
                    stopService();
                }
            }
        });
    }

    private void startService() {

        m_serviceRunning = true;
        startService(new Intent(this, LocationService.class));
        m_mainButton.setText("Stop Background GPS Service");
    }

    private void stopService() {

        m_serviceRunning = false;
        stopService(new Intent(this, LocationService.class));
        m_mainButton.setText("Start Background GPS Service");
    }
}
When I run the service and keep the screen on, I get the correct GPS
latitude and longitude for my position. When I turn the screen off,
however, I get the same repeating output:

01-31 15:48:58.467: D/LocationService(11752): Got a new location frome
the LocationManager [-36.879621, -7.734375]
01-31 15:48:59.467: D/LocationService(11752): Got a new location frome
the LocationManager [-36.879621, -7.734375]
01-31 15:49:00.467: D/LocationService(11752): Got a new location frome
the LocationManager [-36.879621, -7.734375]
01-31 15:49:01.467: D/LocationService(11752): Got a new location frome
the LocationManager [-36.879621, -7.734375]
01-31 15:49:02.477: D/LocationService(11752): Got a new location frome
the LocationManager [-36.879621, -7.734375]
01-31 15:49:03.477: D/LocationService(11752): Got a new location frome
the LocationManager [-36.879621, -7.734375]
01-31 15:49:04.467: D/LocationService(11752): Got a new location frome
the LocationManager [-36.879621, -7.734375]
When I turn the screen back on, I get correct GPS values once more.
Here's the kicker: Google Maps Navigation runs on my phone with the
screen off. If I run it at the same time as my test program, the same
behavior occurs... the Navigation app gets notified of the correct
coordinates while my test app repeats the same lat/long over and over.

Thanks for any input!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to