Android Gets the location null problem

Source: Internet
Author: User

12:38:542016-12-23

Many people often encounter this problem, mainly to obtain the location of the information is null, the first major to have permission

<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name= "Android.permission.ACCESS_MOCK_LOCATION"/>

The second method we call is location location = locationmanager.getlastknownlocation (provider);

This method is mainly to obtain the last latitude and longitude information, the last word must be empty. This is to register the listener that gets the event

After a while you can get the latitude and longitude. So don't worry about getting latitude and longitude at first. Wait a minute. Null pointer exceptions are not reported.

Locationmanager Locationmanager;
String serviceName = Context.location_service;
Locationmanager = (locationmanager) con.getsystemservice (serviceName);//Find service Information
//locationmanager.settestproviderenabled ("GPs", true);

locationmanager.requestlocationupdates (locationmanager.gps_provider, 0, MLocationListener01);
locationmanager.requestlocationupdates (locationmanager.network_provider, 0, MLocationListener01);

Get Data and listeners

* Get the exact location of the current device
*
* @param location
* @param rowId
* @param params
* @param rowId
*/
Private location Updatetonewlocation (location location) {
System.out.println ("--------zhixing--2--------");
String latlongstring;
Double lat = 0;
Double lng=0;

if (location! = null) {
lat = Location.getlatitude ();
LNG = Location.getlongitude ();
latlongstring = "Latitude:" + lat + "\ n Longitude:" + LNG;
System.out.println ("Longitude:" +lng+ "Latitude:" +lat);
} else {
latlongstring = "Unable to obtain geographic information, please later ...";
}
if (lat!=0) {
System.out.println ("--------feedback----------" + string.valueof (LAT));
}

Toast.maketext (Getapplicationcontext (), latlongstring, Toast.length_short). Show ();

 return location;
  
 }
  
       //set listener, the minimum time for automatic Updates is interval n seconds (1 seconds for 1*1000, so write mainly for convenience) or minimum displacement change more than N m
  Public final Locationlistener MLocationListener01 = new Locationlistener () {
      & nbsp   @Override
        public void onlocationchanged (location location) {
             updatetonewlocation (location);
       &NBSP;}
   

@Override
Public void onproviderdisabled (String provider) {
updatetonewlocation (NULL);
 }

@Override
Public void onproviderenabled (String provider) {}

@Override
Public void Onstatuschanged (String provider, int status, Bundle extras) {}
};
Tested: No problem.. Hope to help you ...

Android Gets the location null problem

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.