Android advanced tutorial (14th)-Use of Android location !!

Source: Internet
Author: User

Hello everyone, let's talk about it today.Location
,Location
InAndroid
It is often used during development. For example, you can obtain the weather by longitude and latitude.Location
Obtain region detailsAddress
(For exampleGoogle Map
Development. InAndroid
PassedLocationmanager
To obtainLocation
. Usually getLocation
YesGPS
Get,WiFi
.

I am doing a simple littleDemo
To teach you how to obtainLocation
To obtain the longitude and latitude. The next section will teach you how to useLocation
To obtainAddress
.

 

First step:

 

CreateAndroid
Project name:Locationdemo
.

 

Step 2: ModifyMain. xml
The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: id = "@ + ID/longpolling" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "longpolling:" <br/> <textview <br/> Android: Id = "@ + ID/latitude" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "latitude: "<br/> </linearlayout> <br/>

 

Step 3: ModifyLocationdemo. Java
The Code is as follows:

Package COM. android. tutor; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. location. location; <br/> Import android. location. locationmanager; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; <br/> public class locationdemo extends activity {</P> <p> private textview longpolling; <br/> private textview latitude; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> longpolling = (textview) findviewbyid (R. id. longpolling); <br/> latitude = (textview) findviewbyid (R. id. latitude); </P> <p> location mlocation = getlocation (this); </P> <p> longpolling. settext ("longpolling:" + mlocation. getlongpolling (); <br/> latitude. settext ("latitude:" + mlocation. getlatitude (); <br/>}</P> <p> // get the location by GPS or WiFi <br/> Public location getlocation (context) {<br/> locationmanager locman = (locationmanager) Context <br/>. getsystemservice (context. location_service); <br/> location = locman <br/>. getlastknownlocation (locationmanager. gps_provider); <br/> If (location = NULL) {<br/> location = locman <br/>. getlastknownlocation (locationmanager. network_provider); <br/>}< br/> return location; <br/>}< br/>}

 

Step 4: Add and modify permissionsAndroidmanifest. xml
The Code is as follows (16th rows added by the Action ):

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. android. tutor "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". locationdemo "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "7"/> <br/> <uses-Permission Android: Name = "android. permission. access_fine_location "/> <br/> </manifest>

 

Step 5: RunLocationdemo
Project ):

 

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.