In the previous Android positioning function (I), I have roughly introduced the categories related to the positioning function on the Android platform and obtained location information through examples. However, the previous article customized a standard based on Criteria. Using the getBestProvider () method, the Android system automatically obtains the LocationProvider that best complies with Criteria, thus realizing the positioning function. This approach can maximize the feasibility of the positioning function, but it cannot guarantee that the obtained location information has the maximum accuracy. Because apart from GPS, there are more or less location offsets in other positioning methods.
Before you implement GPS positioning, take a look at some features of GPS:
1. GPS Positioning relies on three or more satellites.
2. GPS positioning is greatly affected by the environment. It is easier to search for satellites in a clear space, while satellites cannot be found indoors.
3. GPS Positioning requires the GPS function module, while the power consumption of the GPS function module is huge.
In the Android system, the idea of GPS Positioning should be:
1. Get the Location Provider of GPS.
2. Introduce this Provider to the requestLocationUpdates () method, so that the Android system can know the search location method.
3. Create an object that implements the GpsStatus. Listener interface, override the onGpsStatusChanged () method, and add the secondary Listener to the LocationManager to detect the satellite status. (Optional)
Based on the above ideas, we can easily obtain the following implementation code following the example in Android positioning function (1): (the premise of implementation of this Code is that the GPS function module is in the Enabled state)
1 public class MainActivity extends Activity {
2 private LocationManager locationManager;
3 private GpsStatus gpsstatus;
4 @ Override
5 public void onCreate (Bundle savedInstanceState ){
6 super. onCreate (savedInstanceState );
7 setContentView (R. layout. main );
8 9 // get the LocationManager object
10 locationManager = (LocationManager) getSystemService (LOCATION_SERVICE );
11
12 // obtain the most compliant provider object based on the configured Criteria object
13 String currentProvider = locationManager. getProvider (LocationManager. GPS_PROVIDER). getName ();
14
15 // obtain the last location information based on the current provider object
16 Location currentLocation = locationManager. getLastKnownLocation (currentProvider );
17 // if the location information is null, request to update the location information
18 if (currentLocation = null ){
19 locationManager. requestLocationUpdates (currentProvider, 0, 0, locationListener );
20} 21 // Add a GPS status listener
22 locationManager. addGpsStatusListener (gpsListener );
23
24 // until the last position is obtained. If the last position is not obtained, the default longitude and latitude are displayed.
25 // obtain the location information every 10 seconds
26 while (true ){
27 currentLocation = locationManager. getLastKnownLocation (currentProvider );
28 if (currentLocation! = Null ){
29 Log. d ("Location", "Latitude:" + currentLocation. getLatitude ());
30 Log. d ("Location", "location:" + currentLocation. getlongdistance ());
31 break;
32} else {
33 Log. d ("Location", "Latitude:" + 0 );
34 Log. d ("Location", "location:" + 0 );
35}
36 try {
37 Thread. sleep (10000 );
38} catch (InterruptedException e ){
39 Log. e ("Location", e. getMessage ());
40}
41}
42}
43
44 private GpsStatus. Listener gpsListener = new GpsStatus. Listener (){
45 // triggered when the GPS status changes
46 @ Override
47 public void onGpsStatusChanged (int event ){
48 // obtain the current status
49 gpsstatus = locationManager. getGpsStatus (null );
50 switch (event ){
51 // events at the first position
52 case GpsStatus. GPS_EVENT_FIRST_FIX:
53 break;
54 // start locating events
55 case GpsStatus. GPS_EVENT_STARTED:
56 break;
57 // send GPS satellite status events
58 case GpsStatus. GPS_EVENT_SATELLITE_STATUS:
59 Toast. makeText (MainActivity. this, "GPS_EVENT_SATELLITE_STATUS", Toast. LENGTH_SHORT). show ();
60 Iterable <GpsSatellite> allSatellites = gpsstatus. getSatellites ();
61 Iterator <GpsSatellite> it = allSatellites. iterator ();
62 int count = 0;
63 while (it. hasNext ())
64 {
65 count ++;
66}
67 Toast. makeText (MainActivity. this, "Satellite Count:" + count, Toast. LENGTH_SHORT). show (); 68 break;
69 // stop the positioning event
70 case GpsStatus. GPS_EVENT_STOPPED:
71 Log. d ("Location", "GPS_EVENT_STOPPED ");
72 break;
73}
74}
75 };
76
77
78 // create a location listener
79 private LocationListener locationListener = new LocationListener (){
80 // call 81 @ Override when the location changes
82 public void onLocationChanged (Location location ){
83 Log. d ("Location", "onLocationChanged ");
84}
85
86 // called when the provider is invalid
87 @ Override
88 public void onProviderDisabled (String provider ){
89 Log. d ("Location", "onProviderDisabled ");
90}
91
92 // called when provider is enabled
93 @ Override
94 public void onProviderEnabled (String provider ){
95 Log. d ("Location", "onProviderEnabled ");
96}
97
98 // called when the status changes
99 @ Override100 public void onStatusChanged (String provider, int status, Bundle extras ){
101 Log. d ("Location", "onStatusChanged ")
102}
103 };
104}
Through the comments in the above code, you can clearly understand the specific meanings of related methods in the Android positioning function. I hope it will be useful to you.
In addition, because of the characteristics of GPS, this code can hardly be located indoors, so we recommend that you use at least two different Location providers, network and GPS, to implement the positioning function in real projects.
At present, I have not found a method to disable the network and GPS functions for locating at the same time, and I have not found code to enable the network and GPS functions without the ROOT function. If you have your own experiences in these two aspects, please do not hesitate to give us any further comments or give us a reference address. We will discuss and make progress together.