Android中使用
Service結(jié)合高德定位
SDK實現(xiàn)定位
高德官方給出的定位實例都是基于Activity的,但是實際過程中會有需要不斷定位的情況,特別是需要后臺統(tǒng)計用戶數(shù)據(jù)的。如果在Activity中綁定定位服務(wù),那么定位就受Activity的生命周期限制,顯然用Service實習(xí)定位是個不錯的選擇。然后結(jié)合BroadcastRecevier可以實現(xiàn)當數(shù)據(jù)需要更新的時候即使提醒提醒更新數(shù)據(jù)。
package cn.letsbook.running.service;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import cn.letsbook.running.model.FixedLengthList;
import cn.letsbook.running.util.Constants;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
/**
* 定位服務(wù),基于高德的定位API V1.3.0實現(xiàn)
*
* @author Jywang
* @email jywangkeep@163.com
*
*/
public class LocationService extends Service implements AMapLocationListener {
public static final long LOCATION_UPDATE_MIN_TIME = 10 * 1000;
public static final float LOCATION_UPDATE_MIN_DISTANCE = 5;
private FixedLengthList<AMapLocation> locationList = FixedLengthList
.newInstance();
// 位置服務(wù)代理
private LocationManagerProxy locationManagerProxy;
public LocationService() {
}
@Override
public void onCreate() {
super.onCreate();
//使用參數(shù)為Context的方法,Service也是Context實例,
//是四大組件之一
locationManagerProxy = LocationManagerProxy.getInstance(this);
// 定位方式設(shè)置為混合定位,包括網(wǎng)絡(luò)定位和GPS定位
locationManagerProxy.requestLocationData(
LocationProviderProxy.AMapNetwork, LOCATION_UPDATE_MIN_TIME,
LOCATION_UPDATE_MIN_DISTANCE, this);
// 如果定位方式包括GPS定位需要手動設(shè)置GPS可用
locationManagerProxy.setGpsEnable(true);
Log.v("locationservice", "locationservicestart");
}
@SuppressWarnings("deprecation")
@Override
public void onDestroy() {
super.onDestroy();
// 在Service銷毀的時候銷毀定位資源
if (locationManagerProxy != null) {
locationManagerProxy.removeUpdates(this);
locationManagerProxy.destory();
}
//設(shè)置為null是為了提醒垃圾回收器回收資源
locationManagerProxy = null;
}
@Override
public void onLocationChanged(Location location) {
//在較新的SDK版本中,這個方法在位置發(fā)生變化的時候不會被
//調(diào)用。這個方法默認是使用原生GPS服務(wù)的時候,當位置
//變化被調(diào)用的方法。
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
//當位置發(fā)生變化的時候調(diào)用這個方法。
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
// 如果位置獲取錯誤則不作處理,退出本方法
// 返回錯誤碼如果為0則表明定位成功,反之則定位失敗
//在虛擬機測試的時候,返回錯誤碼31,為未知錯誤
//如果使用虛擬機測試的時候遇到這個問題,建議使用真機測試。
if (aMapLocation == null
|| aMapLocation.getAMapException().getErrorCode() != 0) {
Log.v("locationservice", aMapLocation == null ? "null" : "not null");
if (aMapLocation != null) {
Log.v("locationservice", "errorcode"
+ aMapLocation.getAMapException().getErrorCode());
Log.v("locationservice", "errormessage"
+ aMapLocation.getAMapException().getErrorMessage());
}
Log.v("locationservice", "request error");
return;
}
//locationList是一個自定義實現(xiàn)的泛型類,
//用于實現(xiàn)定長固定列表的功能。
locationList.addElement(aMapLocation);
Log.v("test", locationList.getLastElement().toString());
// 發(fā)送廣播傳送地點位置信息到地圖顯示界面
// 當數(shù)據(jù)正常獲取的時候,把位置信息通過廣播發(fā)送到接受方,
// 也就是需要處理這些數(shù)據(jù)的組件。
Intent intent = new Intent();
intent.setAction(Constants.INTENT_ACTION_UPDATE_DATA);
intent.putExtra(Constants.INTENT_ACTION_UPDATE_DATA_EXTRA_LATITUDE,
aMapLocation.getLatitude());
intent.putExtra(Constants.INTENT_ACTION_UPDATE_DATA_EXTRA_LONGITUDE,
aMapLocation.getLongitude());
this.sendBroadcast(intent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
其中關(guān)于自定義泛型固定長度列表,可移步這篇文章Java實現(xiàn)泛型單例模式
然后就是需要自己在需要接收數(shù)據(jù)的地方寫BroadcastReceiver實例就行了。為了便于更新控件顯示的數(shù)據(jù),個人建議把BroadcastRecevier當作內(nèi)部類實現(xiàn),如下面代碼所示:
/**
* 自定義廣播接收者類
*
* @author Jywang
* @time 2014-11-4
* @email jywangkeep@163.com
*
*/
public class Receiver extends BroadcastReceiver {
/**
* 空構(gòu)造函數(shù),用于初始化Recevier
*/
public Receiver() {
}
private void disposeUpdateDataAction(Intent intent) {
//書寫更新控件數(shù)據(jù)邏輯
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
if (Constants.INTENT_ACTION_UPDATE_DATA.equals(intent.getAction()))
disposeUpdateDataAction(intent);
else if (Constants.INTENT_ACTION_UPDATE_TIME.equals(intent
.getAction()))
RunCustomActivity.this.updateTime();
}
}// end receiver
然后在Activity(這個是我用于處理數(shù)據(jù)的邏輯,我要把位置信息繪制在地圖上。)的onStart()中注冊廣播,如下所示:
@Override
protected void onStart() {
super.onStart();
initiazlieGPS();
// 每次重新回到界面的時候注冊廣播接收者
IntentFilter filter = new IntentFilter();
filter.addAction(Constants.INTENT_ACTION_UPDATE_DATA);
filter.addAction(Constants.INTENT_ACTION_UPDATE_TIME);
registerReceiver(receiver, filter);
//其他需要處理的邏輯
}
然后在Activity的onStop()方法中解除對廣播的注冊,如一下代碼所示:
@Override
protected void onStop() {
super.onStop();
if (receiver != null)
unregisterReceiver(receiver);
}
其中用到的receiver是我建立的一個Receiver這個自定義廣播接收類的一個實例。
當你需要啟動定位服務(wù)的時候,使用下面代碼啟動Service:
Intent startLocationServiceIntent = new Intent(this,
LocationService.class);
startService(startLocationServiceIntent);
需要停止定位服務(wù)的時候,使用下面代碼停止Service:
Intent stopLocationServiceIntent = new Intent(this,
LocationService.class);
stopService(stopLocationServiceIntent);
對了,Service一定要在AndroidManifest.xml文件中聲明,否則無法使用。如下所示:
<!-- 定位服務(wù) -->
<service
android:name="cn.letsbook.running.service.LocationService"
android:exported="false" />
這段代碼放到application內(nèi)部,跟Activity同一個層級。
以上就是全部。