我試圖編寫一些AR應用程序。現在我寫了一些代碼來顯示相機預覽,並從設備上的傳感器(acceleromentr,compas,gps reciver)獲取數據。使用CameraPreview時獲取GPS數據的問題
當我在單獨的應用程序中運行代碼(如相機預覽作爲一個應用程序和獲取gps數據作爲第二個應用程序),一切正常。但是,當我嘗試整合這兩個模塊 - GPS停止工作;它看起來像聽衆沒有得到任何數據。你有沒有類似的問題?
的代碼看起來是這樣的:
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");
}
}
};
我試圖註冊在的onCreate /的onPause /註銷監聽器,但行爲是一樣的。
的cameraPreview代碼看起來像:
私人SurfaceHolder.Callback mSurfaceHolderCallback =新SurfaceHolder.Callback() { 私人相機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;
}
}
};
,這是註冊的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在layout.xml定義爲
SurfaceView android:id="@+id/CameraSurface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
任何想法有什麼不好?
的代碼是在三個不同的電話
LocationListener在什麼時候停止接收事件?它在某個時候是否再次開始接收事件? – 2010-10-23 19:40:52