Andriod automatic network switching and gps Positioning

Source: Internet
Author: User
Tags timedelta


After obtaining the Location service, request the network and gps Location updates at the same time, and then the Location information of the network and gps will be reported at the same time. When there is no gps signal, the location information of the Network Positioning is automatically obtained. If there is a gps signal, the location information provided by gps is obtained first. isBetterLocation determines whether to update the current Location information based on the time, accuracy, and positioning method. This method is derived from the Obtaining User Location in the Development Guide.


Package cncit. gps;


Import java. text. SimpleDateFormat;
Import java. util. Date;


Import android. app. Activity;
Import android. content. Context;
Import android. location. Location;
Import android. location. LocationListener;
Import android. location. LocationManager;
Import android. OS. Bundle;
Import android. util. Log;
Import android. widget. TextView;


Public class UploadgpsActivity extends Activity
{
LocationManager lm = null;
Location myLocation = null;
TextView loc, timeText;


SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss. SSSZ ");


@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );


Loc = (TextView) findViewById (R. id. loc );
TimeText = (TextView) findViewById (R. id. time );
Lm = (LocationManager) getSystemService (Context. LOCATION_SERVICE );
}


@ Override
Protected void onResume ()
{
Super. onResume ();


Lm. requestLocationUpdates (LocationManager. NETWORK_PROVIDER, 0, 0,
Listener );
Lm. requestLocationUpdates (LocationManager. GPS_PROVIDER, 0, 0,
Listener );


Log. e ("onResume", "onResume ");
}


@ Override
Protected void onPause ()
{
Super. onPause ();


Log. e ("onPause", "onPause ");


Lm. removeUpdates (listener );
}


 


LocationListener listener = new LocationListener ()
{


@ Override
Public void onLocationChanged (Location location)
{
// Actual reporting time
// String time = sdf. format (new Date (location. getTime ()));
// TimeText. setText ("actual reporting time:" + time );
 
If (isBetterLocation (location, myLocation ))
{
// Obtain the latitude
Double lat = location. getLatitude ();
// Obtain the longitude
Double lon = location. getlongdistance ();
// Location provider
String provider = location. getProvider ();
// Accuracy of location
Float accuracy = location. getAccuracy ();
// Height Information
Double altitude = location. getAltitude ();
// Direction
Float bearing = location. getBearing ();
// Speed meter/second
Float speed = location. getSpeed ();

String locationTime = sdf. format (new Date (location. getTime ()));
String currentTime = null;
 
If (myLocation! = Null)
{
CurrentTime = sdf. format (new Date (myLocation. getTime ()));
MyLocation = location;

}
Else
{
MyLocation = location;
}

Loc. setText ("longitude:" + lon
+ "\ N latitude:" + lat
+ "\ N service provider:" + provider
+ "\ N accuracy:" + accuracy
+ "\ N Height:" + altitude
+ "\ N Direction:" + bearing
+ "\ N speed:" + speed
+ "\ N last reported time:" + currentTime
+ "\ N latest reporting time:" + locationTime );


}

 

 

}


@ Override
Public void onStatusChanged (String provider, int status, Bundle extras)
{
Log. e ("onStatusChanged", "onStatusChanged:" + provider );


}


@ Override
Public void onProviderEnabled (String provider)
{
Log. e ("onProviderEnabled", "onProviderEnabled:" + provider );
}


@ Override
Public void onProviderDisabled (String provider)
{
Log. e ("onProviderDisabled", "onProviderDisabled:" + provider );
}


};


Private static final int TWO_MINUTES = 1000*1*2;


/**
* Determines whether one Location reading is better than the current
* Location fix
*
* @ Param location
* The new Location that you want to evaluate
* @ Param currentBestLocation
* The current Location fix, to which you want to compare the new
* One
*/
Protected boolean isBetterLocation (Location location,
Location currentBestLocation)
{
If (currentBestLocation = null)
{
// A new location is always better than no location
Return true;
}


// Check whether the new location fix is newer or older
Long timeDelta = location. getTime ()-currentBestLocation. getTime ();
Boolean isSignificantlyNewer = timeDelta> TWO_MINUTES;
Boolean isSignificantlyOlder = timeDelta <-TWO_MINUTES;
Boolean isNewer = timeDelta> 0;


// If it's been more than two minutes since the current location, use
// The new location
// Because the user has likely moved
If (isSignificantlyNewer)
{
Return true;
// If the new location is more than two minutes older, it must be
// Worse
}
Else if (isSignificantlyOlder)
{
Return false;
}


// Check whether the new location fix is more or less accurate
Int accuracyDelta = (int) (location. getAccuracy ()-currentBestLocation
. GetAccuracy ());
Boolean isLessAccurate = accuracyDelta> 0;
Boolean isMoreAccurate = accuracyDelta <0;
Boolean isSignificantlyLessAccurate = accuracyDelta> 200;


// Check if the old and new location are from the same provider
Boolean isFromSameProvider = isSameProvider (location. getProvider (),
CurrentBestLocation. getProvider ());


// Determine location quality using a combination of timeliness and
// Accuracy
If (isMoreAccurate)
{
Return true;
}
Else if (isNewer &&! IsLessAccurate)
{
Return true;
}
Else if (isNewer &&! IsSignificantlyLessAccurate & isFromSameProvider)
{
Return true;
}
Return false;
}


/** Checks whether two providers are the same */
Private boolean isSameProvider (String provider1, String provider2)
{
If (provider1 = null)
{
Return provider2 = null;
}
Return provider1.equals (provider2 );
}


}


Author: qq1761321372

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.