What is wrong with this Thread? It only gets runned one time. (Thread with GPS Location)
i have a activity that must capture GPS position of the user each 500ms and write it on the screen (Textview tv2). But something is going wrong
It only captures the GPS position sent by the DDMS of the emulator one time, and i put some log prints on the code ( method run()
) to check the Thread and i saw that the prints are only writted ONE TIME on the log cat.
Also the last log print never get's called.... wtf. I mean this print: Log.w("PABLO", "despues loop");
it never get's called.... it's like the thread stops after Looper.loop or something.
This is the code:
public class AugmentedRealitySampleActivity extends Activity implements Runnable{
private TextView tv2;
//variables para obtener mi posicion:
LocationManager mLocationManager;
Location mLocation;
MyLocationListener mLocationListener;
Location currentLocation = null;
double lat=-1;
double lon=-1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FrameLayout rl = new FrameLayout(this.getApplicationContext());
LinearLayout ll= new LinearLayout(this.getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(rl);
rl.addView(ll);
tv2=new TextView(getApplicationContext());
tv2.setBackgroundColor(Color.BLACK);
ll.addView(tv2);
tv2.setText("Test2");
Log.w("PABLO", "on create");
Thread thread = new Thread(this);
thread.start();
}
////////////////////////////////////////////////////////////////////////
//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))
{
Log.w("PABLO", "principio hilo");
Looper.prepare();
mLocationListener = new MyLocationListener();
try{
Log.w("PABLO", "antes mLocationManager");
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, mLocationListener);
Log.w("PABLO", "despues mLocationManager");
}catch(Exception e){}
//try {
// wait(100);
//}catch (InterruptedException e) {}
Log.w("PABLO", "antes loop");
Looper.loop();
Log.w("PABLO", "despues 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);
}
}
}
};
}
Either the last log call is not flushed, the Looper.loop() does not end, or a runtime exception is thrown. I would first add a catch on Throwable around Looper.loop(). As these things must be obvious to you too, I would look for (1) simple non-reentrance errors, (2) leaking resources, especially unclosed files, URL connections.
精彩评论