Why i can't get my GPS position?
i'm trying to get my GPS position and update with it a textview (tv2) that it is on the top of the Activity. My Activity shows the camera view and captures GPS coordenates and it haves to write them on the textview. It also get's compass & accelerometer data, but it is not the problem now.
Something is wrong because i can't capture the GPS data... but i dont understand what is wrong. If i create a new project from scratch and paste the GPS location request code it get's the code correctly, but here on this app i can't get the GPS positions... with the same code. ¿what is going wrong?
for the cameraView i'm using the default cameraView code written on this guide: http://www.devx.com/wireless/article/42482/1954
and this is my main class:
public class AugmentedRealitySampleActivity extends Activity implements Runnable{
private CustomCameraView cv=null;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private TextView tv5;
private TextView tv6;
public static SensorManager sensorMan;
//variables para obtener mi posicion:
LocationManager mLocationManager;
Location mLocation;
MyLocationListener mLocationListener;
Location currentLocation = null;
private float direction; //compass
//private Location curLocation; //gps
public volatile float inclination; //accelerometer
double lat=-1;
double lon=-1;
public void onCreate(Bundle savedInstanceState)
{
//try{
super.onCreate(savedInstanceState);
cv = new CustomCameraView(this.getApplicationContext());
FrameLayout rl = new FrameLayout(this.getApplicationContext());
LinearLayout ll= new LinearLayout(this.getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(rl);
rl.addView(cv);
rl.addView(ll);
//} catch(Exception e){}
tv1=new TextView(getApplicationContext());
tv2=new TextView(getApplicationContext());
tv3=new TextView(getApplicationContext());
tv4=new TextView(get开发者_StackOverflowApplicationContext());
tv5=new TextView(getApplicationContext());
tv6=new TextView(getApplicationContext());
tv1.setBackgroundColor(Color.BLACK);
tv2.setBackgroundColor(Color.BLACK);
//tv3.setBackgroundColor(Color.BLACK);
//tv4.setBackgroundColor(Color.BLACK);
//tv5.setBackgroundColor(Color.BLACK);
//tv6.setBackgroundColor(Color.BLACK);
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tv3);
ll.addView(tv4);
ll.addView(tv5);
ll.addView(tv6);
sensorMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensorMan.registerListener(Compasslistener, sensorMan.getDefaultSensor(SensorManager.SENSOR_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
sensorMan.registerListener(Accelerometerlistener, sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
//LocationManager locMan;
//locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, gpsListener);
//getLatitude(); getLongitude(); bearingTo(); distanceTo();
tv1.setText("Test1");
tv2.setText("Test2");
tv3.setText("Test3");
tv4.setText("Test4");
tv5.setText("Test5");
tv6.setText("Test6");
Thread thread = new Thread(this);
thread.start();
}
////////////////////////////////////////////////
//COMPASS:
////////////////////////////////////////////////
SensorEventListener Compasslistener = new SensorEventListener()
{
public void onAccuracyChanged(Sensor arg0, int accuracy){ }
public void onSensorChanged(SensorEvent evt)
{
float vals[] = evt.values;
direction = vals[0];
tv1.setText("Direction= " + direction);
}
};
////////////////////////////////////////////////
//ACCELEROMETER:
////////////////////////////////////////////////
private SensorEventListener Accelerometerlistener = new SensorEventListener()
{
public volatile float direction = (float) 0;
//public volatile float rollingZ = (float)0;
public volatile float kFilteringFactor = (float)0.05;
public float aboveOrBelow = (float)0;
public void onAccuracyChanged(Sensor arg0, int arg1){}
public void onSensorChanged(SensorEvent evt)
{
float vals[] = evt.values;
if(evt.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
float rawDirection = vals[0];
direction =(float) ((rawDirection * kFilteringFactor) + (direction * (1.0 - kFilteringFactor)));
inclination = (float) ((vals[2] * kFilteringFactor) + (inclination * (1.0 - kFilteringFactor)));
if(aboveOrBelow > 0)
inclination = inclination * -1;
if(evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
aboveOrBelow = (float) ((vals[2] * kFilteringFactor) + (aboveOrBelow * (1.0 - kFilteringFactor)));
tv3.setText("Inclination= " + inclination);
tv4.setText("Direction2= " + direction);
tv5.setText("kFilteringFactor= " + kFilteringFactor);
tv6.setText("aboveOrBelow= " + aboveOrBelow);
}
}
};
////////////////////////////////////////////////////////////////////////
//Métodos del Hilo que obtiene la posicion GPS del usuario periodicamente.
///////////////////////////////////////////////////////////////////////
public void run() {
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Looper.prepare();
mLocationListener = new MyLocationListener();
try{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200, 0, mLocationListener);
}catch(Exception e){}
//try {
// wait(100);
//}catch (InterruptedException e) {}
Looper.loop();
}
}
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
if (loc != null) {
try{
currentLocation = loc;
handler.sendEmptyMessage(0);
}catch(Exception e){}
}
}
public void onProviderDisabled(String provider) { }
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (currentLocation!=null)
{
lat=currentLocation.getLatitude();
lon=currentLocation.getLongitude();
if (lat==-1 && lon ==-1) /// si no existe ninguna posicion GPS anterior en el telefono, no hago nada
{
}
else //// si existe alguna posicion anterior (es decir, si el GPS del telefono ha sido activado al menos alguna vez en su vida util)
{
tv2.setText("Location= "+lat+" "+lon);
}
}
}
};
}
Did you put in your AndroidManifest the permission?
Like:
<android.permission.ACCESS_COARSE_LOCATION" />
<android.permission.ACCESS_FINE_LOCATION" />
or see more permissions in the android developer documentation: http://developer.android.com/reference/android/Manifest.permission.html
I think it's some kind of bug of the phone, it can't works fine with accelerometer, compass and GPS being used at the same time
精彩评论