这篇教程主要介绍了在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
)