We recommend that you update the ADT version 20130522, either in Linux or windows. I think the speed is much faster. After upgrading the android SDK tool to Ver 22, there are many unknown reasons for the original ADT. The connection may fail when the app is loaded to the simulator.
Allows applications to be positioned
Location is a major feature of mobile phone functions. In addition to GPS, satellite navigation also includes Galileo in Europe and Beidou in China. In addition, it also includes triangular positioning of the operator's mobile base station and WiFi hotspot positioning provided by third parties. For related recommendations, read the CDMA Positioning method. The base station signal strength is used in a triangle-specific way (affected by building blocking and reflection in the urban area), and the geographic location of the AP on the Internet is used for positioning.
Location. The precise positioning of satellites is fine location. To enable the positioning function of an application, you must first grant permission.
<Uses-Permission Android: Name = "android. permission.Access_coarse_location"/>
<Uses-Permission Android: Name = "android. permission.Access_fine_location"/>
Set the location in the simulator
We can set the latitude and longitude in the simulator through ddms, and modify the latitude and longitude in the program running. Window-> open perspective-> ddms
Example of continuous location acquisition
The following example is simple. The UI is a textview used to display information. We keep track of the device location, that is, tracking our path.
Public class locationbase extends activity {
Private textview mytext;
Private string textstr = "";
PrivateLocationmanagerMgr = null;
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. pure_text_view );
MyText = (TextView) findViewById (R. id. pure_text );
AddText ("Location Based-Services Demo ...");
// Step 1: Obtain the positioning manager of the system
Mgr = (locationmanager) getsystemservice (Location_service);
AddText ("get system Location Manager" + mgr );
}
//Step 2.2Stop update in continuous tracing: In this example, we require that the location update be continuously obtained. We must manually perform removeUpdates (); otherwise, even if all the activities in the application are disabled, the App continues to update its location information, which prevents application resources from being recycled.
Protected voidOnpause(){
Super. onPause ();
Mgr.Removeupdates(OnLocationChange );
AddText ("Remove Updates ...");
}
// Step 2: Set continuous Tracing
//Step 2.1 (1): We keep tracing in onResume (), and close the tracing in onPause () accordingly. We only perform tracking when the Activity is running. For details, see the lifecycle of an Activity.
Protected voidOnresume(){
Super. onResume ();
//Steps 2.1 (2 ):Since the person's location is constantly changing, I want to set a range of location changes, including meeting the minimum interval and minimum displacement changes at the same time. If both conditions must be met at the same time, the location listener will be triggered. In fact, this method has multiple parameter formats, especially requestlocationupdates (long mintime, float mindistance, Criteria,Pendingintent
Intent), when the position changes, other activities can be called. In this example, if GPS is used, the permission must be precisely located.
Mgr.Requestlocationupdates(Locationmanager. gps_provider, 10000/* 10 seconds, for test convenience */, 1000/* 1 km */, onlocationchange/* location listener */);
Addtext ("request updates automatically ...");
}
// Step 3: Set the location listener locationlistener. When the location changes, onlocationchanged () is triggered ()
Locationlistener onlocationchange = new locationlistener (){
Public voidOnlocationchanged(Location ){
Addtext ("location changed: (" +
Location. getlongdistance () + "," + location. getlatitude () + ")");
}
Public voidOnproviderdisabled(String arg0 ){
AddText ("onProviderDisabled ");
}
Public voidOnproviderenabled(String arg0 ){
AddText ("onProviderEnabled ");
}
Public voidOnstatuschanged(String arg0, int arg1, Bundle arg2 ){
AddText ("onStatusChanged ");
}
};
Private void addText (String s ){
TextStr = textStr. concat (s + "\ n ");
MyText. setText (textStr );
}
}
We may find that the onLocationChanged () of the location monitor is not started at the beginning. In reality, the location information may not be obtained immediately because the GPS is not enabled or the location is just opened. In the simulator, we can press the "send" button in DDMS. To change the location information, press "send ".
Selection of positioning
In this example, we specify that GPS is used. In fact, location retrieval can be obtained through satellites, carriers, and third-party services, that is, multiple information sources. The system can query available location information sources as follows:
List <String> list = mgr. getAllProviders (); // mgr: LocationManager
For (Iterator <String> I = list. iterator (); I. hasNext ();){
System. out. println ("\ t" + I. next ());
}
We can re-select, or let the user choose, but the better way is to let the system help select. As follows:
Criteria criteria = new Criteria ();
String providerName = mgr.Getbestprovider(Criteria, true
/* Enabledonly */); // criteria cannot be null; otherwise, an exception occurs.
Locationprovider provider = Mgr. getprovider (providername );
We can add some conditions to criteria, such as setaccuracy (), setaltituderequired (), and setcostallowed. If all the conditions are not met, the best one will be matched for you. Of course, if you set a free price, you will not be charged for it. We get the locationprovider object according to the provider name, but generally we don't need it. We can request location information from the system by knowing the provider name.
Location = Mgr.Getlastknownlocation(Providername );
If (location! = NULL)
System. Out. println ("Get location from" + providername + ":" + location. getlatitude () + "," + location. getlongdistance ());
Android uses getlastknownlocation () to query, rather than the current real-time information. It may be unable to be obtained due to some circumstances, for example, if GPS is not enabled or is being started, null may be returned. The returned information may not only have latitude and longitude, but also whether there is altitude information. Use location. hasaltitude () to check whether there is speed information. Use hasspeed () to query.
Triggered when a reminder arrives at a certain position.
A notification is triggered when the user's location is near a target. This can be used in many scenarios. In locationmanager, you can use addproximityalert () to call pendingintent near a location.
Public void addproximityalert (double latitude/* Dimension */,
Double longpolling/* precision */,
Float radius/* radius */,
Long Expiration/* is invalid.-1 indicates that it is not invalid. You can use removeproximityalert () to manually delete it */,
Pendingintent intent/* trigger Processing */)
Compare two location data
Location obtained before and after (may come from different providers, or the conditions for obtaining location information may change, for example, finding one more satellite or missing one satellite, which leads to a significant difference in precision between the front and back). We can determine which location is better representative of the current location. We can use location. gettime () is used to obtain the Update Time of location information. getaccruacy () is used to obtain the precision, and location is used. getprovider () in turn gets providername for provider comparison. Through these comparisons, we can select data that is not the best. Provided on the android developer website
Select the location.
Related links:
My android development articles