Generally, mobile phones with Android systems are equipped with GPS devices. Android provides a good interface for us to obtain GPS data. This article describes how to use Android to obtain the longitude and latitude of GPS.
1. inherit a class from the Service.
2. Create the startService () method.
3. Create the endService () method and reload the onCreate and onDestroy methods. Call startService and endService in these two methods.
4. In startService, use the getSystemService method to obtain Context. LOCATION_SERVICE.
5. implement a new class based on LocationListener. By default, the four methods onLocationChanged, onProviderDisabled, onProviderEnabled, and onStatusChanged are reloaded. The onLocationChanged method is the method for updating the latest GPS data. Generally, we only need to process operations here.
6. Call the requestLocationUpdates method of LocationManager to periodically obtain GPS data. In the onLocationChanged function, we can perform the final operation on the obtained longitude and latitude.
7. Start the Service and stop the Service in our Activity by clicking the button.
The sample code is as follows:
Package com. offbye. gpsservice;
Import android. app. Service;
Import android. content. Context;
Import android. content. Intent;
Import android. location. LocationListener;
Import android. location. LocationManager;
Import android. OS. Binder;
Import android. OS. IBinder;
Import android. util. Log;
Public class GPSService extends Service {
// 2000 ms
Private static final long minTime = 2000;
// Minimum change distance: 10 m
Private static final float minDistance = 10;
String tag = this. toString ();
Private LocationManager locationManager;
Private LocationListener locationListener;
Private final IBinder mBinder = new GPSServiceBinder ();
Public void startService (){
LocationManager = (LocationManager) getSystemService (Context. LOCATION_SERVICE );
LocationListener = new GPSServiceListener ();
LocationManager. requestLocationUpdates (LocationManager. GPS_PROVIDER, minTime, minDistance,
LocationListener );
}
Public void endService (){
If (locationManager! = Null & locationListener! = Null ){
LocationManager. removeUpdates (locationListener );
}
}
@ Override
Public IBinder onBind (Intent arg0 ){
// TODO Auto-generated method stub
Return mBinder;
}
@ Override
Public void onCreate (){
//
StartService ();
Log. v (tag, "GPSService Started .");
}
@ Override
Public void onDestroy (){
EndService ();
Log. v (tag, "GPSService Ended .");
}
Public class GPSServiceBinder extends Binder {
GPSService getService (){
Return GPSService. this;
}
}
}
Implementation of GPSServiceListener
Package com. offbye. gpsservice;
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import java. util. GregorianCalendar;
Import java. util. TimeZone;
Import android. location. Location;
Import android. location. LocationListener;
Import android. location. LocationProvider;
Import android. OS. Bundle;
Import android. util. Log;
Import android. widget. Toast;
Public class GPSServiceListener implements LocationListener {
Private static final String tag = "GPSServiceListener ";
Private static final float minAccuracyMeters = 35;
Private static final String hostUrl = "http://doandroid.info/gpsservice/position.php? ";
Private static final String user = "huzhangyou ";
Private static final String passed = "123456 ";
Private static final int duration = 10;
Private final DateFormat timestampFormat = new SimpleDateFormat ("yyyyMMddHHmmss ");
Public int GPSCurrentStatus;
@ Override
Public void onLocationChanged (Location location ){
// TODO Auto-generated method stub
If (location! = Null ){
If (location. hasAccuracy () & location. getAccuracy () <= minAccuracyMeters ){
// Obtain the time parameter and Post the time to the server.
GregorianCalendar greg = new GregorianCalendar ();
TimeZone tz = greg. getTimeZone ();
Int offset = tz. getOffset (System. currentTimeMillis ());
Greg. add (Calendar. SECOND, (offset/1000) *-1 );
StringBuffer strBuffer = new StringBuffer ();
StrBuffer. append (hostUrl );
StrBuffer. append ("user = ");
StrBuffer. append (user );
StrBuffer. append ("& pass = ");
StrBuffer. append (pass );
StrBuffer. append ("& Latitude = ");
StrBuffer. append (location. getLatitude ());
StrBuffer. append ("& longpolling = ");
StrBuffer. append (location. getlongpolling ());
StrBuffer. append ("& Time = ");
StrBuffer. append (timestampFormat. format (greg. getTime ()));
StrBuffer. append ("& Speed = ");
StrBuffer. append (location. hasSpeed ());
DoGet (strBuffer. toString ());
Log. v (tag, strBuffer. toString ());
}
}
}
// Send the data to the server through get. The server can track the user's walking status based on the data.
Private void doGet (String string ){
// TODO Auto-generated method stub
//
}
@ Override
Public void onProviderDisabled (String provider ){
// TODO Auto-generated method stub
}
@ Override
Public void onProviderEnabled (String provider ){
// TODO Auto-generated method stub
}
@ Override
Public void onStatusChanged (String provider, int status, Bundle extras ){
// TODO Auto-generated method stub
GPSCurrentStatus = status;
}
}
From offbye's technical blog