Android enables GPS navigation and obtains location information. return null. Solution

Source: Internet
Author: User

Recently, I am working on an Android project and need to use GPS to obtain location information. I checked the information from the API and found that obtaining location information only requires an extremely simple sentence:

Copy codeThe Code is as follows: getLastKnownLocation (LocationManager. GPS_PROVIDER ),

So I was so happy. But once written into the code, the return value (Location type) is always null .. very depressing. After checking on the Internet for a long time, I found that many people are entangled in this issue like me. Some people say that GPS is not enabled, and some say that the related permissions are not added .. however, my settings are already enabled, and permissions are added. After struggling with the api for a long time, I finally found out the cause. The reason for turning on GPS was actually:
 Copy codeThe Code is as follows:
SetTestProviderEnabled ("gps", true );

It has little to do with the settings on the mobile phone (at least on my mobile phone ). If the settings on the mobile phone are disabled, this sentence can still be opened. Even if the phone settings are turned on, none of this sentence is useless. Which of the following statements corresponds
 Copy codeThe Code is as follows:
SetTestProviderEnabled ("gps", false );

Used to disable GPS.
Can I use the following method to obtain the Location after GPS is enabled? No! To be exact, sometimes yes, because this function obtains the location information obtained last time. If this program runs for the first time, it does not obtain the location information before. Of course, the return value is null. After carefully viewing the apiCopy codeThe Code is as follows: requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

It may take a while to receive the most recent location. if an immediate location is required, applications may use the getLastKnownLocation (String) method. therefore, to obtain the Location information, you should use this method to set the listener for the manager and obtain it in onLocationChanged (location Location) of the listener.
The test code is as follows:Copy codeThe Code is as follows: public void onLocationChanged (Location location)
{
Log. I ("onLocationChanged", "come in ");
If (location! = Null)
{
Log. w ("Location", "Current altitude =" + location. getAltitude ());
Log. w ("Location", "Current latitude =" + location. getLatitude ());
}
}

After testing, you can obtain the Location after a period of time (the acquisition time is related to minTime and minDistance ). You must also note that after setting the listener, you cannot use the following method to disable gps before deleting the listener; otherwise, an error is reported. So the method to disable gps is:
 Copy codeThe Code is as follows:
Manager. removeUpdates (listener); // listener is the listener instance.
Manager. setTestProviderEnabled ("gps", false );

The following is the test code with the required permissions:Copy codeThe Code is as follows: <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_MOCK_LOCATION"> </uses-permission>
<Uses-permission android: name = "android. permission. UPDATE_DEVICE_STATS"> </uses-permission>

Copy codeThe Code is as follows: import android. app. Activity;
Import android. content. Context;
Import android. location. Criteria;
Import android. location. Location;
Import android. location. LocationListener;
Import android. location. LocationManager;
Import android. OS. Bundle;
Import android. util. Log;
Public class audio extends Activity
{
/** Called when the activity is first created .*/
LocationManager locationManager;
LocationListener llistener;
String provider;
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Criteria criteria = new Criteria ();
Criteria. setAccuracy (Criteria. ACCURACY_FINE );
Criteria. setAltitudeRequired (false );
Criteria. setBearingRequired (false );
Criteria. setCostAllowed (true );
Criteria. setPowerRequirement (Criteria. POWER_LOW );
String serviceName = Context. LOCATION_SERVICE;
LocationManager = (LocationManager) getSystemService (serviceName );
LocationManager. setTestProviderEnabled ("gps", true );
Provider = locationManager. getBestProvider (criteria, true );
Log. d ("provider", provider );
Llistener = new LocationListener (){
@ Override
Public void onLocationChanged (Location location)
{
// TODO Auto-generated method stub
Log. I ("onLocationChanged", "come in ");
If (location! = Null)
{
Log. w ("Location", "Current altitude ="
+ Location. getAltitude ());
Log. w ("Location", "Current latitude ="
+ Location. getLatitude ());
}
LocationManager. removeUpdates (this );
LocationManager. setTestProviderEnabled (provider, false );
}
@ Override
Public void onProviderDisabled (String provider)
{
// TODO Auto-generated method stub
Log. I ("onProviderDisabled", "come in ");
}
@ Override
Public void onProviderEnabled (String provider)
{
// TODO Auto-generated method stub
Log. I ("onProviderEnabled", "come in ");
}
@ Override
Public void onStatusChanged (String provider, int status,
Bundle extras)
{
// TODO Auto-generated method stub
Log. I ("onStatusChanged", "come in ");
}
};
LocationManager. requestLocationUpdates (provider, 1000, (float) 1000.0, llistener );
}
Protected void onDestroy ()
{
LocationManager. removeUpdates (llistener );
LocationManager. setTestProviderEnabled (provider, false );
Super. onDestroy ();
}

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.