Android開(kāi)發(fā)之Location用法實(shí)例分析
本文實(shí)例講述了Android開(kāi)發(fā)中Location用法。分享給大家供大家參考,具體如下:
Location 在Android 開(kāi)發(fā)中還是經(jīng)常用到的,如通過(guò)經(jīng)緯度獲取天氣,根據(jù)Location 獲取所在地區(qū)詳細(xì)Address (比如Google Map 開(kāi)發(fā))等。而在Android 中通過(guò)LocationManager來(lái)獲取Location .通常獲取Location 有GPS 獲取,WIFI 獲取。
這邊介紹一個(gè)簡(jiǎn)單的小Demo ,來(lái)教大家如何獲取Location ,從而獲取經(jīng)緯度。
第一步:創(chuàng)建一個(gè)Android 工程命名為L(zhǎng)ocationDemo .
第二步:修改main.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/longitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="longitude:" /> <TextView android:id="@+id/latitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="latitude:" /> </LinearLayout>
第三步:修改LocationDemo.Java ,代碼如下:
package pku.ss; import pku.ss.R; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; public class LocationDemoActivity extends Activity { private TextView longitude; private TextView latitude; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); longitude = (TextView)findViewById(R.id.longitude); latitude = (TextView)findViewById(R.id.latitude); Location mLocation = getLocation(this); longitude.setText("Longitude: " + mLocation.getLongitude()); latitude.setText("Latitude: " + mLocation.getLatitude()); } //Get the Location by GPS or WIFI public Location getLocation(Context context) { LocationManager locMan = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); Location location = locMan .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) { location = locMan .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } return location; } }
第四步:增加權(quán)限,修改AndroidManifest.xml 代碼如下(第16行為所增行):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pku.ss" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".LocationDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> </manifest>
第五步:運(yùn)行LocationDemo 工程,所得效果如下:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門與進(jìn)階教程》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android貝塞爾曲線實(shí)現(xiàn)加入購(gòu)物車拋物線動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線實(shí)現(xiàn)加入購(gòu)物車拋物線動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Android view滑動(dòng)懸浮固定效果實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Android view滑動(dòng)懸浮固定效果實(shí)現(xiàn)代碼示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Android中AndroidStudio&Kotlin安裝到運(yùn)行過(guò)程及常見(jiàn)問(wèn)題匯總
這篇文章主要介紹了Android(AndroidStudio&Kotlin)安裝到運(yùn)行過(guò)程及常見(jiàn)問(wèn)題匯總,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒借鑒價(jià)值,需要的朋友可以參考下2020-03-03Android實(shí)現(xiàn)畫(huà)板功能(二)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)畫(huà)板功能的第二篇,使用imageView,bitmap方式實(shí)現(xiàn)畫(huà)板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05android 完全退出應(yīng)用程序?qū)崿F(xiàn)代碼
這篇文章主要介紹了在android中完全退出應(yīng)用的實(shí)現(xiàn)代碼,多種實(shí)現(xiàn)方法,大家可以根據(jù)需求選擇2013-06-06Android實(shí)現(xiàn)TCP斷點(diǎn)上傳 后臺(tái)C#服務(wù)接收
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)TCP斷點(diǎn)上傳,后臺(tái)C#服務(wù)實(shí)現(xiàn)接收,感興趣的小伙伴們可以參考一下2016-08-08詳解Flutter網(wǎng)絡(luò)圖片本地緩存的實(shí)現(xiàn)
這篇文章主要為大家介紹了詳解Flutter網(wǎng)絡(luò)圖片本地緩存的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04