Java code
Package android. test;
Import java. util. Iterator;
Import android. app. Activity;
Import android. location. Criteria;
Import android. location. GpsSatellite;
Import android. location. GpsStatus;
Import android. location. Location;
Import android. location. LocationListener;
Import android. location. LocationManager;
Import android. OS. Bundle;
Import android. location. GpsStatus. Listener;
Import android. location. GpsStatus;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. Toast;
Public class TestActivity extends Activity implements LocationListener
{
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Log. I (tag, "on Create ");
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Btn = (Button) findViewById (R. id. btn );
Tv1 = (TextView) findViewById (R. id. tv1 );
Tv2 = (TextView) findViewById (R. id. tv1 );
Listener = new GpsStatus. Listener (){
Public void onGpsStatusChanged (int event ){
Gpsstatus = mgr. getGpsStatus (null );
Switch (event)
{
Case GpsStatus. GPS_EVENT_FIRST_FIX: gpsstatus. getTimeToFirstFix ();
Case GpsStatus. GPS_EVENT_SATELLITE_STATUS:
// Obtain information about all received satellites, including the satellite height angle, azimuth angle, signal-to-noise ratio, and Pseudo Random Number (and Satellite Number)
Iterable <GpsSatellite> allSatellites;
AllSatellites = gpsstatus. getSatellites ();
Iterator it = allSatellites. iterator ();
String msg = "";
While (it. hasNext ())
{
GpsSatellite oSat = (GpsSatellite) it. next ();
Msg = "azimuth:" + oSat. getAzimuth ();
Msg + = "\ nprn:" + oSat. getPrn ();
Msg + = "\ nsnr:" + oSat. getSnr ();
}
Tv2.setText (msg );
Break;
Case GpsStatus. GPS_EVENT_STARTED:
// Event sent when the GPS system has started.
Break;
Case GpsStatus. GPS_EVENT_STOPPED:
// Event sent when the GPS system has stopped.
Break;
Default:
Break;
}
}
};
Btn. setOnClickListener (new Button. OnClickListener ()
{
@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Log. I (tag, "on button click ");
Getdata ();
Show ();
}
});
}
Void show ()
{
String msg = "";
Msg + = "latitude:" + latitude;
Msg + = "\ nlongtitude:" + longtitude;
Msg + = "\ naltitude:" + altitude;
Msg + = "\ naccuracy:" + accuracy;
Msg + = "\ nbearing:" + bearing;
Msg + = "\ nspeed:" + speed;
Msg + = "\ ntime:" + time;
Tv1.setText (msg );
// Toast. makeText (this, msg, Toast. LENGTH_SHORT). show ();
}
GpsStatus. Listener listener;
Private GpsStatus gpsstatus;
Private final String tag = "TEST_MAY ";
Private LocationManager mgr;
Private Button btn;
Private TextView tv1, tv2;
Private String best;
Private double latitude;
Private double longtitude;
Private double altitude;
Private float accuracy;
Private float bearing;
Private float speed;
Private long time;
Private void getdata ()
{
Try
{Mgr = (LocationManager) getSystemService (LOCATION_SERVICE );
Mgr. addGpsStatusListener (listener );
Criteria criteria = new Criteria ();
Best = mgr. getBestProvider (criteria, true );
Location location = mgr. getLastKnownLocation ("gps ");
If (location. hasAccuracy () accuracy = location. getAccuracy ();
Log. d (tag, "getdata ");
If (location. hasAltitude () altitude = location. getAltitude ();
If (location. hasBearing () bearing = location. getBearing ();
If (location. hasSpeed () speed = location. getSpeed ();
Log. d (tag, "getsomedata ");
Latitude = location. getLatitude ();
Longtitude = location. getlongpolling ();
Time = location. getTime ();
}
Catch (NullPointerException e)
{
}
}
@ Override
Public void onLocationChanged (Location location ){
// TODO Auto-generated method stub
Toast. makeText (this, location. toString (), Toast. LENGTH_SHORT). show ();
}
@ Override
Public void onProviderDisabled (String provider ){
// TODO Auto-generated method stub
Toast. makeText (this, provider + "disable", Toast. LENGTH_SHORT). show ();
}
@ Override
Public void onProviderEnabled (String provider ){
// TODO Auto-generated method stub
Toast. makeText (this, provider + "enable", Toast. LENGTH_LONG). show ();
}
@ Override
Public void onStatusChanged (String provider, int status, Bundle extras ){
// TODO Auto-generated method stub
Toast. makeText (this, status + "changed", Toast. LENGTH_SHORT). show ();
}
}
Author: geek Network"