tags:

views:

71

answers:

2

Hi,

I tried to write some AR app. For now I wrote some code that display camera prewiew and gets data from sensors on the device (acceleromentr, compas, gps reciver).

When I run the code in separate app (like camera preview as one app and application that gets gps data as a second) everything is OK. But when I try to integrate this two modules - GPS stop working; it looks like the listener doesn't get any data. Did You had some similar problems?

The code looks like this:

public void onResume()
{
    super.onResume();

    mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                            1000, 0.0f, 
                                            mLocationListener);
}


public void onPause()
{
    super.onPause();
    mLocationManager.removeUpdates(mLocationListener);
}


private LocationListener mLocationListener = new LocationListener()
{

    public void onLocationChanged(Location pLocation) {
        double lLatitude = pLocation.getLatitude();
        double lLongitude = pLocation.getLongitude();
        mGpsTextView.setText ("Longitude" + Double.toString(lLongitude) + " Latitude: " + Double.toString(lLatitude));
    }

    public void onProviderDisabled(String pProvider) {
        mGpsTextView.setText ("Provider disabled");
    }

    public void onProviderEnabled(String pProvider) {
        mGpsTextView.setText ("Provider enabled");
    }

    public void onStatusChanged(String pProvider, int pStatus, Bundle pExtras) {
        switch (pStatus)
        {
         case LocationProvider.OUT_OF_SERVICE:
             mGpsTextView.setText("GPS out of service");
             break;
         case LocationProvider.TEMPORARILY_UNAVAILABLE:
             mGpsTextView.setText("GPS temorarily unawalible");
             break;
         case LocationProvider.AVAILABLE:
             mGpsTextView.setText("GPS avalible");
             break;
         default:
             mGpsTextView.setText("EEE");
        }

   }

};

I tried to register/unregister listener in onCreate/onPause but the behaviour is the same.

The cameraPreview code looks like:

private SurfaceHolder.Callback mSurfaceHolderCallback = new SurfaceHolder.Callback() { private Camera mCamera;

    public void surfaceCreated (SurfaceHolder pSurfaceHolder)
    {
        stopAndReleaseCamera();
        mCamera = Camera.open();
        try {
            mCamera.setPreviewDisplay(pSurfaceHolder);
        }
        catch (Exception imLazy) {
            stopAndReleaseCamera();
            imLazy.printStackTrace();
        }
    }

    public void surfaceChanged (SurfaceHolder pSurfaceHolder, int pFormat, int pWidth, int pHeight)
    {
        Parameters lCameraParams = mCamera.getParameters();
        lCameraParams.setPreviewSize (pWidth, pHeight);
        mCamera.setParameters (lCameraParams);
        mCamera.startPreview();
    }

    public void surfaceDestroyed (SurfaceHolder pSurfaceHolder)
    {
        stopAndReleaseCamera();
    }

    private void stopAndReleaseCamera()
    {
        if (mCamera != null)
        {
            try
            {
                mCamera.stopPreview();
                mCamera.release();
            }
            catch (Exception imLazy)
            {
                //ignore
                imLazy.printStackTrace();
            }
            mCamera = null;
        }
    }
};

And this is registered in onCreate:

    SurfaceView lSurfaceView = (SurfaceView)findViewById(R.id.CameraSurface);
    lSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    lSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    lSurfaceView.getHolder().addCallback (mSurfaceHolderCallback);

R.id.CameraSurface is defined in layout.xml as

SurfaceView android:id="@+id/CameraSurface"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"

Any ideas what is wrong?

The code was tested on three different phones

A: 

Have you tried to move your LocationListener Stuff in a Service which runs in background? Maybe Android needed more ressources for Camera Preview and stopped your LocationListener.

Henry
A: 

If you need a steady and continous stream of location updates you have to make sure that the LocationListener is NOT deallocated. When the listener dies it will be detected (through remote interface) by system the LocationManager, and if no other LocationListeners is listening, the system LocationManager will stop! The result is that the GPS has to aquire a new fix. As Henry mentioned: a service will do the trick.

If you have a physical phone you can check the behavior by running your GPS code and rotate the phone.

NielsProsch