Android gps is one of the simple and practical

Source: Internet
Author: User

The gps function provided by android allows you to conveniently obtain location information. Because this is the gps used in the simulator again, the gps location information should be sent through ddms.
The main. xml file has only one textview node and is not provided. The following permissions are required to operate gps:
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
The specific operation code is as follows:
MainActivity. java
[Java] view plaincopyprint?
Package com. example. locationmanager;
 
Import android. app. Activity;
Import android. content. Context;
Import android. location. Location;
Import android. location. LocationListener;
Import android. location. LocationManager;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. widget. TextView;
 
Public class MainActivity extends Activity {

Private TextView TV _show;
// Locate the manager
Private LocationManager lm;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

TV _show = (TextView) this. findViewById (R. id. TV _show );
// Obtain the positioning manager provided by the system
Lm = (LocationManager) getSystemService (Context. LOCATION_SERVICE );
// Obtain the latest location information from gps
Location location = lm. getLastKnownLocation (LocationManager. GPS_PROVIDER );
// Use location to display information
UpdateView (location );
// Set the listener
Lm. requestLocationUpdates (LocationManager. GPS_PROVIDER, 3000, 8, new LocationListener (){

@ Override
Public void onStatusChanged (String provider, int status, Bundle extras ){
 
}

@ Override
Public void onProviderEnabled (String provider ){
// Update the location when gps is available
UpdateView (lm. getLastKnownLocation (provider ));
}

@ Override
Public void onProviderDisabled (String provider ){
UpdateView (null );
}

@ Override
Public void onLocationChanged (Location location ){
// Update the location when the gps positioning information changes'
UpdateView (location );
}
});

}
 
Private void updateView (Location location ){
If (location! = Null ){
StringBuffer sb = new StringBuffer ();
Sb. append ("implementation location information: \ n ");
Sb. append ("precision :");
Sb. append (location. getlongpolling ());
Sb. append ("\ n-dimensional :");
Sb. append (location. getLatitude ());
Sb. append ("\ n Height :");
Sb. append (location. getAltitude ());
Sb. append ("\ n speed :");
Sb. append (location. getSpeed ());
Sb. append ("\ n Direction :");
Sb. append (location. getBearing ());
TV _show.setText (sb. toString ());
} Else {
TV _show.setText ("");
}
}
 
}

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.