Android Open GPS Navigation and get location information return NULL solution _android

Source: Internet
Author: User
Tags stub

Recently in doing an Android project, need to use GPS to obtain location information, check from the API, found that access to location information only need a very simple sentence can:

Copy Code code as follows:

Getlastknownlocation (Locationmanager.gps_provider),

So happy. But in the code, the return value (Location type) is always null. It's so depressing. Check on the internet for a long time, found that a lot of people and I have the same tangled in this issue, some people say because the GPS did not open, but also some people say that the relevant permissions did not add. But my obviously already opened in the setting, the permission nature also added. Entangled in the API for a long time, and finally find out why, the original to open the GPS is actually in this sentence:
 
Copy Code code as follows:
 
Settestproviderenabled ("GPs", true);

It has little to do with the settings on the phone (at least on my phone). The device on the phone is off, and this one is still open, and even if the phone set is turned on, there is no such thing. And this corresponds to the sentence
 
Copy Code code as follows:
 
Settestproviderenabled ("GPs", false);

Used to turn off the GPS.
Can the above method get the location after the GPS is opened? Or not! It is sometimes possible, because this function gets the location information that was obtained last time, assuming that if the program first ran, the location information was not previously acquired, and the return value is null, of course. After careful review of the API, the
Copy Code code as follows:

Requestlocationupdates (String provider, long mintime, float mindistance, Locationlistener listener)

Found in this sentence: it may take a while to receive the most recent location. If an immediate location be required, applications may use the Getlastknownlocation (String) method. Therefore, in order to obtain location information, you should set up the listener for the manager in this way, onlocationchanged (Location Location) in the listener.
The test code is as follows:
Copy Code code 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, after a period of time can obtain location (acquisition time and Mintime, mindistance related). Also need to note that after setting the listener, remove the listener can not use the above method to turn off the GPS, otherwise it will be an error. So the way to close the GPS is
 
Copy Code code as follows:
 
Manager.removeupdates (listener);//listener is the listener instance
Manager.settestproviderenabled ("GPs", false);

The following are the test code, and the required permissions are:
Copy Code code 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 Code code 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 the activity is a. */
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.