如何在syncadapter中使用位置监听器?

时间:2016-07-26 09:23:31

标签: android gps android-syncadapter

如何在这里使用带有syncadapter的位置监听器是我的疑问我需要每30分钟更新一次背景中的用户位置,所以我打算使用syncadapter,当应用程序在后台运行时,它会在常规间隔运行,而不是获得纬度和逻辑值相反,我得到像空指针一样的异常,我得到纬度和经度这是一个坏方法或有没有比这更好的方法(注意:我在我的应用程序中使用syncadapter所以我正在使用该syncadapter)任何人都可以在这里分享他们的想法让我发布了迄今为止我尝试过的内容:

Here is the Location listener class:
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

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

    // =======================================================
    // private vars
    // =======================================================
    private GoogleApiClient moGoogleApiClient;
    private LocationRequest moLocationRequest;
    private Location moCurrentLocation;
    private static final int kTIMEOUT_MILLISECONDS = 2500;
    Context context;

    // =======================================================
    // public static vars
    // =======================================================

    // =======================================================
    // public methods
    // =======================================================

    public void Start(Context oContext) {
        context=oContext;
        if (moGoogleApiClient == null) {
            moGoogleApiClient = new GoogleApiClient.Builder(oContext)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        if (moLocationRequest == null) {
            moLocationRequest = new LocationRequest();

            moLocationRequest.setInterval(1);
            moLocationRequest.setFastestInterval(1);
            moLocationRequest.setInterval(1);
            moLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        }
        // Start the connection
        if (moGoogleApiClient != null) {
            if (!moGoogleApiClient.isConnecting() && !moGoogleApiClient.isConnected())
                moGoogleApiClient.connect();
            else if (moCurrentLocation == null)

                LocationServices.FusedLocationApi.requestLocationUpdates(moGoogleApiClient, moLocationRequest, this);

        }
    }

    public void Stop() {
        if (moGoogleApiClient != null && moGoogleApiClient.isConnected())
            LocationServices.FusedLocationApi.removeLocationUpdates(moGoogleApiClient, this);
        if (moGoogleApiClient != null)
            moGoogleApiClient.disconnect();
    }

    public Location GetLocationBlocking(Context oContext) throws InterruptedException {
        if (moCurrentLocation == null) {
            int intTimeout = kTIMEOUT_MILLISECONDS;
            Start(oContext);
            while (intTimeout > 0) {
                Thread.sleep(100);
                intTimeout -= 100;
            }
            Stop();
        }

  //      String lat=String.valueOf(moCurrentLocation.getLatitude());
    //    String longt=String.valueOf(moCurrentLocation.getLongitude());
//        Toast.makeText(oContext,"Lat"+lat+"long"+longt,Toast.LENGTH_SHORT).show();
        return moCurrentLocation;

    }

    // =======================================================
    // Location API Events
    // =======================================================

    @Override
    public void onLocationChanged(Location oLocation) {
        if (oLocation != null) {
            moCurrentLocation = oLocation;
        }
    }

    // =======================================================
    // Google API Connection Events
    // =======================================================


    @Override
    public void onConnected(Bundle connectionHint) {
        // Connected to Google Play services! The good stuff goes here.
        if (moGoogleApiClient != null) {

            Location oLocation = LocationServices.FusedLocationApi.getLastLocation(moGoogleApiClient);
            if (oLocation != null)
                moCurrentLocation = oLocation;
            else
                LocationServices.FusedLocationApi.requestLocationUpdates(moGoogleApiClient, moLocationRequest, this);
        }
    }

    @Override
    public void onConnectionSuspended(int cause)
    {
    }

    @Override
    public void onConnectionFailed(ConnectionResult result)
    {
        String errmessage=result.getErrorMessage();
        Log.d("errormesage",errmessage);
    }

这里的onPerformSync方法使用如下:

   cSyncLocation cSyncLocation=new cSyncLocation();
            try {
                Location loc=cSyncLocation.GetLocationBlocking(getContext());
                if(loc==null){
                    Log.e("no value","no value");
                }
                else {
                    Log.d("lat", String.valueOf(loc.getLatitude()));
                    Log.d("long", String.valueOf(loc.getLongitude()));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

即使在处于锁定状态的手机中,如果能够完成它,我是否需要定位细节的有效方式...

0 个答案:

没有答案