1. 程式人生 > >實現高德地圖後臺持續定位

實現高德地圖後臺持續定位

 高德地圖實現在後臺持續定位,當然需要服務,但是現在已經無法在殺死APP的時候仍然持續獲取了,我這種形式只能在APP沒被殺死的時候持續獲取,獲取到定位資訊後你可以回調出去,下面就是我這個服務

public class LocationService extends Service {

    private static final String TAG = "LocationService";
    //宣告AMapLocationClient類物件
    AMapLocationClient mLocationClient = null;
    // 宣告AMapLocationClientOption物件
    public AMapLocationClientOption mLocationOption = null;

    public LocationService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getPosition();
    }

    public void getPosition() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        // 設定定位回撥監聽
        mLocationClient.setLocationListener(mLocationListener);
        // 初始化AMapLocationClientOption物件
        mLocationOption = new AMapLocationClientOption();
        // 設定定位模式為AMapLocationMode.Hight_Accuracy,高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //設定定位間隔,單位毫秒,預設為2000ms
        mLocationOption.setInterval(60000);
        // 獲取一次定位結果: //該方法預設為false。
        mLocationOption.setOnceLocation(false);
        mLocationOption.setOnceLocationLatest(false);
        //給定位客戶端物件設定定位引數
        mLocationClient.setLocationOption(mLocationOption);
        // 啟動定位
        mLocationClient.startLocation();
    }

    // 宣告定位回撥監聽器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation == null) {
                Log.i(TAG, "amapLocation is null!");
                return;
            }
            if (amapLocation.getErrorCode() != 0) {
                Log.i(TAG, "amapLocation has exception errorCode:" + amapLocation.getErrorCode());
                return;
            }
            Double longitude = amapLocation.getLongitude();//獲取經度
            Double latitude = amapLocation.getLatitude();//獲取緯度
            String longitudestr = String.valueOf(longitude);
            String latitudestr = String.valueOf(latitude);
            Log.i(TAG, "longitude:" + longitude + ",latitude:" + latitude);
        }
    };
}

 新增這個服務需要在清單檔案註冊

<service
            android:name=".service.LocationService"
            android:enabled="true"
            android:exported="true" />

        <!--處理bug:amapLocation has exception errorCode:10-->
        <service android:name="com.amap.api.location.APSService" />
    </application>

如果不加 APSService這個會報錯,加上就可以了

然後就是去啟動服務了

 public void startAlarm() {
        //首先獲得系統服務
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        //設定鬧鐘的意圖,我這裡是去呼叫一個服務,該服務功能就是獲取位置並且上傳
        Intent intent = new Intent(this, LocationService.class);
        PendingIntent pendSender = PendingIntent.getService(this, 0, intent, 0);
        am.cancel(pendSender);
        //AlarmManager.RTC_WAKEUP ;這個引數表示系統會喚醒程序;設定的間隔時間是1分鐘
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pendSender);
    }