在后台服务中使用融合位置获取GPS更新前(15分钟)?

时间:2016-10-18 15:36:43

标签: android gps android-service fusedlocationproviderapi

我对于Android开发的新手需要在没有用户交互的情况下在后台服务中获取gps位置,例如每15分钟需要获取坐标并将结果发送到服务器我该如何实现这一点。然后我尝试融合位置api syncadapter组合。它工作,我得到坐标但我需要在专用的android-service中使用该位置类如何实现这个需要使该服务运行15分钟获取坐标并将结果发送到服务器让我发布我的谷歌API客户端类。请检查我尝试过的下面的代码。

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.text.DateFormat;
import java.util.Date;

/**
 * Created by 4264 on 14-10-2016.
 */

public class Locationlistener implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,LocationListener  {


    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mCurrentLocation;
    String mLastUpdateTime;
    private Context context;
    private static final long INTERVAL = 1000 * 10;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    public Locationlistener(Context c)
    {
        context=c;
        //show error dialog if GoolglePlayServices not available
        createLocationRequest();
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
    }
    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }
    public String lat(){
        String lat=null;
        if(mCurrentLocation==null){
            Log.d("is null","null");
        }
        else {
            lat=String.valueOf(mCurrentLocation.getLatitude());
        }
        return lat;
    }

    protected void startLocationUpdates() {
        PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
        Log.d("started", "Location update started ..............: ");
    }
    public void updateUI() {
        Log.d("updated", "UI update initiated .............");
        if (null != mCurrentLocation) {
            String lat = String.valueOf(mCurrentLocation.getLatitude());
            String lng = String.valueOf(mCurrentLocation.getLongitude());
            Log.e("latw",lat);
            Log.e("Long",lng);

        } else {
            Log.d("", "location is null ...............");
        }
    }


    @Override
    public void onConnected(Bundle bundle) {
        Log.d("connected", "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
        startLocationUpdates();
    }


    @Override
    public void onConnectionSuspended(int i) {

    }




    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d("failed", "Connection failed: " + connectionResult.toString());
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("locationchanged", "Firing onLocationChanged..............................................");
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUI();
    }
}

它适用于syncadapter,但我需要在服务中如何实现这一点,如果用户关闭gps,我将如何获得位置的另一个疑问是否可以通过网络位置更新提前感谢!!

1 个答案:

答案 0 :(得分:0)

我建议您尽快使用AlarmManager唤醒设备并接收位置(间隔0),然后每15分钟继续睡眠。所以这种方法比不间断服务更好。

如果用户关闭gps,您仍将获得(网络提供商)位置更新,但由于位置设置不充分,因此不准确。另外,要解决此问题,您可以使用SettingsApi

  

在向位置服务发出请求时,设备的系统设置可能处于阻止应用获取其所需位置数据的状态。例如,可以关闭GPS或Wi-Fi扫描。