這篇教程主要介紹了在Android平臺上如何使用服務完成定位功能。衆所周知,Android設備的當前位置信息,對開發創新性App、解決人們平常生活問題有極大幫助。在Android平臺開發定位相關的應用程序,須要位置提供者。有兩種類型的位置提供者:java
以上兩種類型,任何一種均可以得到用戶或者用戶設備的位置信息。可是,它們各有優劣,推薦二者同時使用。GPS 定位,在室內反應遲緩,比較耗時;網絡定位,在沒有網絡的時候沒法得到位置信息。android
Manifest
文件中受權,接收定位數據。LocationManager
實例,將其指向定位服務。LocationManager
請求定位數據。LocationListener
接收更新的定位數據。在Manifest文件中獲取以下權限,而後能夠經過定位提供者得到定位數據:git
<manifest ... > <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
定位提供者必須要有INTERNET
權限和ACCESS_FINE_LOCATION
權限。同時,網絡定位還須要ACCESS_COARSE _LOCATION
權限。segmentfault
不管何種類型的Android後臺Service,須要得到其引用才能使用。一樣,經過getSystemService()
方法得到定位服務的引用,而後將這個引用將添加到新建立的LocationManager
實例中,示例以下:網絡
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
穿件位置服務引用後,經過LocationManager
的requestLocationUpdates()
方法能夠請求位置更新信息。調用方法時,須要位置提供者、最後一次更新距今的時間(秒)、距離和LocationListener
對象。調用後LocationListener
對象會根據位置進行更新。app
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
根據指定的距離或時間間隔,LocationListener會收到更新通知。ide
這個示例經過GPS定位獲取當前位置數據。主要代碼以下:佈局
package com.javapapers.android.geolocationfinder; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.TextView; import android.util.Log; public class MainActivity extends Activity implements LocationListener { protected LocationManager locationManager; protected LocationListener locationListener; protected Context context; TextView txtLat; String lat; String provider; protected String latitude, longitude; protected boolean gps_enabled, network_enabled; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtLat = (TextView) findViewById(R.id.textview1); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Override public void onLocationChanged(Location location) { txtLat = (TextView) findViewById(R.id.textview1); txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude()); } @Override public void onProviderDisabled(String provider) { Log.d("Latitude", "disable"); } @Override public void onProviderEnabled(String provider) { Log.d("Latitude", "enable"); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.d("Latitude", "status"); } }
佈局文件以下,this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout>
Manifest文件以下,spa
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.javapapers.android.geolocationfinder" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" > <activity android:name="com.javapapers.android.geolocationfinder.MainActivity" 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> </manifest>
提示:若是使用模擬器運行這個示例,須要將準確的經緯度發送到模擬器。
Window>Open Perspective
)