Android opens the GPS navigation and gets the location information back to the null solution

  • 2020-05-07 20:28:41
  • OfStack

Recently, I have been working on an Android project, and I need to use GPS to obtain location information. After looking up 1 from API, I found that to obtain location information, it only needs a very simple sentence:


getLastKnownLocation(LocationManager.GPS_PROVIDER),

So he was very happy. However, when 1 is written into the code, the return value (Location type) is 1 directly null.. I'm so depressed. I have searched the Internet for a long time, and found that many people are entangled with me on this issue. Some people said that it was because GPS was not opened, while others said that the relevant permissions were not added.. But my obviously already opened in the setting, the permission also added naturally. After struggling with api for a long time, I finally figured out the reason why I should open GPS.
 
setTestProviderEnabled("gps",true); 

It doesn't have much to do with the Settings on my phone (at least on my phone). If the Settings on the phone are turned off, this sentence will still open. And even if the phone's Settings are turned on, this sentence is useless. The answer to this sentence is
 
setTestProviderEnabled("gps",false);

To close GPS.
When GPS is open, can you use the above method to get Location? Still not! Sometimes, exactly, because this function gets the location information it got last time. Imagine that if this program runs the first time and didn't get the location information before, it returns null. After a close look at api, in

requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) 

Found this sentence: It may take a while to receive the recent location. If an immediate location required, applications may the getLastKnownLocation(String) method. This method should be used to set the listener for manager, which is obtained in onLocationChanged(Location location) in the listener.
The test 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, Location can be obtained after 1 period of time (the acquisition time is related to minTime and minDistance). Another problem to note is that after the listener is set, gps cannot be turned off by the above method before the listener is removed, otherwise an error will be reported. So the way to close gps is
 
manager.removeUpdates (listener);//listener  Is the listener instance 
manager.setTestProviderEnabled("gps",false);

The following is the test code, the required permissions are:

<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>


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 articles: