First, the reference source:
GPS Basic example-android Example
Http://androidexample.com/GPS_Basic__-__Android_Example/index.php?view=article_discription&aid=68&aaid =93
Second, Permission:
<android:name= "Android.permission.ACCESS_FINE_LOCATION"/> <android:name= "Android.permission.INTERNET"/>
Third, Example:
PackageCom.example.gpsbasics;Importandroid.location.Location;ImportAndroid.location.LocationListener;ImportAndroid.location.LocationManager;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.widget.Toast;Importandroid.app.Activity;ImportAndroid.content.Context; Public classMainactivityextendsActivityImplementsLocationlistener {PrivateLocationmanager Locationmanager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); /********** get Gps location Service Locationmanager Object ***********/ /********** Get GPS Service Management Object ************/Locationmanager=(Locationmanager) Getsystemservice (Context.location_service); /*Parameters:first (Provider): The name of the provider with which to register : Registered name Second (mintime): The minimum time interval for notifications, in milliseconds . This field was only used as a hint to conserve power, and actual time between location updates could be greater or lesser tha n this value. : The minimum notification interval, in milliseconds. This field is used only as a power saver, and the actual time between location updates can be larger or smaller than the value. Third (mindistance): The minimum distance interval for notifications, in meters: Minimum interval notification , in milliseconds fourth (listener): a {#link locationlistener} whose onlocationchanged (location) method will is call Ed for every location update: the Onlocationchanged (location) method is called when each position is updated*/locationmanager.requestlocationupdates (Locationmanager.gps_provider,3000,//3 sec10, This); /********* After registration onlocationchanged method called periodically after each 3 sec ***********/ } /************* called after each 3 sec **********/@Override Public voidonlocationchanged (location location) {//location.getlatitude (): Latitude//location.getlongitude (): DimensionString str = "Latitude:" +location.getlatitude () + "\nlongitude:" +Location.getlongitude (); Toast.maketext (Getbasecontext (), str, toast.length_long). Show (); LOG.E ("Gpsbasics", "onlocationchanged."); } @Override Public voidonproviderdisabled (String provider) {/******** called when User off Gps *********/Toast.maketext (Getbasecontext (),"Gps turned off", Toast.length_long). Show (); } @Override Public voidonproviderenabled (String provider) {/******** called when User on Gps *********/Toast.maketext (Getbasecontext (),"Gps turned on", Toast.length_long). Show (); } @Override Public voidOnstatuschanged (String provider,intstatus, Bundle extras) { //TODO auto-generated Method Stub }}
Android GPS Gpsbasics Project hacking