Analyzes two permission requests for Android Enhancement

Source: Internet
Author: User

To complete the Android enhancement task, we mainly use the Android LocationManager object. Before performing operations on this project, we need to solve some other Android problems, permission is the first obstacle we need to remove first.

Android-enhanced LocationManager supports two types of permission requests:

1. You need to inform the system of your desired location for the user.
2. You need to tell it that you want detailed geographical information.

You need to get the location manager in the AndroidManifest. xmlxml file <manifes seems simple, but you still have to remember some things. First, we may only request the location manager in the main UI thread. We either request the LocationManager object in the onCreate call of the action, or use the LocationManager request to create an executable object running on the main thread.

As you can see, here we declare a LocationManager object, use getSystemService to get your object, and then call requestLocationUpdates. You may want to know which parameters are required when the location is updated. First, you tell the system that you want to use the location update function of the GPS device in the system. Then.

You tell it how long you want to update (in this example, the interval is 100 ms), and it is updated whenever you move more than one meter. In this way, you can quickly identify their movements and adjust their positional relationships with other objects. Finally, input an instance of the class that implements the LocationListener interface. After a request is sent for location update, the LocationListener class receives the initial location and changes the location later. Here is our LocationListener:

 
 
  1. LocationListener gpsListener = new LocationListener(){  
  2.       Location curLocation;  
  3.       boolean locationChanged = false;   
  4.       public void onLocationChanged(Location location)  
  5.       {  
  6.          if(curLocation == null)  
  7.          {  
  8.             curLocation = location;  
  9.             locationChanged = true;  
  10.          }  
  11.            
  12.          if(curLocation.getLatitude() == location.getLatitude() &&  
  13.                curLocation.getLongitude() == location.getLongitude())  
  14.             locationChanged = false;  
  15.          else  
  16.             locationChanged = true;  
  17.            
  18.          curLocation = location;  
  19.       }  
  20.       public void onProviderDisabled(String provider){}  
  21.       public void onProviderEnabled(String provider){}  
  22.       public void onStatusChanged(String provider, int status, Bundle extras){}  
  23. }; 

In the code above, the onLocationChanged method is the only thing we need to care about. However, we will introduce other methods of this object. So that you can understand this object when copying it to your own code. Once the satellite locks the device, the method onLocationChanged will be called, and each time after the specified interval (100 ms in this example) in request updating, it will be called once.

Every time the Location is updated, the Android enhancement function will bring a Location object. Through this class, we can obtain the longitude and latitude of the target and accomplish many important tasks. Here we are most interested in getLatitude (), getlongdistance (), bearingTo () and distanceTo (). Using these four functions, we can calculate the azimuth of any subsequent positions and determine the distance from you.

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.