Creare un nuovo progetto android chiamato HelloLocation Creare un layout con due campi di tipo EditText che conterranno le coordinate <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:layout_marginTop="40dip"> <TextView android:text="Latitudine: " android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView01" android:layout_marginLeft="10dip" android:layout_marginRight="5dip" android:textSize="20dip" /> <TextView android:id="@+id/latitudine" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dip" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_height="wrap_content" android:layout_width="match_parent"> <TextView android:text="Longitudine: " android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView03" android:layout_marginLeft="10dip" android:layout_marginRight="5dip" android:textSize="20dip" /> <TextView android:id="@+id/longitudine" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20dip"></TextView> </LinearLayout> </LinearLayout> Implementazione dell'activity public class HelloLocationActivity extends Activity implements LocationListener { private TextView latitudine; private TextView longitudine; private LocationManager locationManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); latitudine = (TextView) findViewById(R.id.latitudine); longitudine = (TextView) findViewById(R.id.longitudine); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { int lat = (int) (location.getLatitude()); int lng = (int) (location.getLongitude()); latitudine.setText(String.valueOf(lat)); longitudine.setText(String.valueOf(lng)); } else { latitudine.setText("Provider non disponibile"); longitudine.setText("Provider non disponibile"); } } @Override protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this); } @Override protected void onPause() { super.onPause(); locationManager.removeUpdates(this); } @Override public void onLocationChanged(Location location) { int lat = (int) (location.getLatitude()); int lng = (int) (location.getLongitude()); latitudine.setText(String.valueOf(lat)); longitudine.setText(String.valueOf(lng)); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } } Per testare il programma viene utilizzata una classe di test che invia delle coordinate random package tcweb.android.test; import java.io.IOException; import java.security.SecureRandom; import com.oroinc.net.telnet.TelnetClient; public class FakeGPS { public final static void main(String[] args) throws Exception { final TelnetClient telnet = new TelnetClient(); telnet.connect("localhost", 5554); Thread reader = new Thread() { public void run() { int ch; try{ while(!interrupted() && (ch = telnet.getInputStream().read()) != -1) { System.out.print(Character.valueOf((char)ch)); } }catch (IOException e) { // TODO: handle exception } } }; reader.start(); for (int i = 0; i < 100; i++) { Thread.sleep(3000); int lat = new SecureRandom().nextInt(50); int lon = new SecureRandom().nextInt(50); String cmd = "geo fix "+lat+" "+lon+"\n"; telnet.getOutputStream().write(cmd.getBytes()); telnet.getOutputStream().flush(); System.out.print(cmd); } telnet.disconnect(); System.exit(0); } }
|
|||||||||
