0

Google Play Services を使用して現在の場所を見つけています。しかし、それはどのコールバック関数にも入っていません。Google API クライアントに接続できません。

LocationUtilクラスの私のコードは次のとおりです。

package com.steporganization.util;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

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.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

/**
 * Created by Yogesh on 26-07-2015.
 */
public class LocationUtil implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener{
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
    private final static String TAG="LocationUtil_Yogesh";
    private GoogleApiClient client;
    private Location location;
    private boolean isClientConnected=false;

    private Activity baseActivity;
    private Context context;

    private boolean isPlayServicesAvailable()
    {
        int resultCode= GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        if (resultCode!= ConnectionResult.SUCCESS)
        {
           if(GooglePlayServicesUtil.isUserRecoverableError(resultCode))
           {
               GooglePlayServicesUtil.getErrorDialog(resultCode,baseActivity,PLAY_SERVICES_RESOLUTION_REQUEST).show();
               return false;
           }
            else
           {
            Toast.makeText(context,
                    "This device is not supported.", Toast.LENGTH_LONG)
                    .show();
               return false;
           }
        }
        return true;
    }


    private synchronized void buildGoogleAPiClient()
    {
        client=new GoogleApiClient.Builder(context).
                addConnectionCallbacks(this).addOnConnectionFailedListener(this).
                addApi(LocationServices.API).build();


    }

    private Location getLocation()
    {
        Location lastLocation;
        lastLocation= LocationServices.FusedLocationApi.getLastLocation(client);
        if(lastLocation==null)
        {
            //check for location
            //requestlocation update until some location is acquired or timeout of 10 seconds
            Long initialTime= System.currentTimeMillis();
            Long finalTime=System.currentTimeMillis();
            createLocationRequest();
            while(this.location==null || finalTime-initialTime<20000)
            {
                finalTime=System.currentTimeMillis();
            }
            if(this.location==null)
            {
                Log.d(TAG,"Unable to fetch location");
                LocationServices.FusedLocationApi.removeLocationUpdates(client,this);
                return null;
            }
            else
                LocationServices.FusedLocationApi.removeLocationUpdates(client,this);
                return this.location;

        }
        else
            return lastLocation;
    }

    @Override
    public void onConnected(Bundle bundle) {

        Log.d(TAG,"onConnected Called");
        isClientConnected=true;
        location=getLocation();

    }

    @Override
    public void onConnectionSuspended(int i) {

        Log.d(TAG,"onConnection suspended Called");
        client.connect();


    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d(TAG,"onConnection Failed Called");
        Log.d(TAG, "Error Code: " + connectionResult.getErrorCode());

    }

    protected void createLocationRequest()
    {
        LocationRequest locationRequest=new LocationRequest().
                setInterval(1000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        LocationServices.FusedLocationApi.requestLocationUpdates(client, locationRequest, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        this.location=location;
    }





    public Location getUserCurrentLocation(Activity activity,Context context)
    {
        this.baseActivity=activity;
        this.context=context;
        if(isPlayServicesAvailable())
        {
            Log.d(TAG, "play services available");
            buildGoogleAPiClient();
            if(client!=null)
            {
                client.connect();
            }
            else
            {
                Log.d(TAG,"Cleint null");
            }
            while(isClientConnected!=true)
            {
                Log.d(TAG,"connecting API client");
            }
            return this.location;

        }
        else {
            Log.d(TAG, "play service not available");
            return null;
        }
    }
}

別のアクティビティでは、このクラスのオブジェクトを作成してgetUserCurrentLocation(this,getApplicationContext())関数を呼び出しています。

ここで私が間違っていることを誰かに教えてもらえますか? また、それは正しいコードではありませんが、要件に応じてロジックを変更できるように、コールバック関数内に到達しない理由が私のポイントです。

また、同様の質問を見つけましたが、まだ回答がありません。

4

0 に答える 0