I tried to write some kind of AR application. At the moment, I wrote a code that displays a preview of the camera and receives data from sensors on the device (accelerator, compass, gps reciver).
When I run the code in a separate application (for example, viewing the camera as one application and an application that receives gps data per second), everything is in order. But when I try to integrate these two modules - GPS stops working; it looks like the listener is not receiving any data. Have you had some similar problems?
The code is as follows:
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 registering / unregistering the listener in onCreate / onPause, but the behavior is the same.
CameraPreview code looks like this:
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) {
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 has been tested on three different phones.