Google融合位置API最后已知位置,时间约束为5秒?

时间:2015-02-11 19:00:03

标签: android google-play-services google-client

我关注此事:https://developer.android.com/training/location/retrieve-current.html。这是我的代码:

public class CameraActivity extends Activity implements PictureCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {


    protected GoogleApiClient mGoogleApiClient;


    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        Log.i(TAG,"last location"+ mLastLocation);     //logs correctly

        if (mLastLocation != null) {
            mLatitude = String.valueOf(mLastLocation.getLatitude());
            mLongitude = String.valueOf(mLastLocation.getLongitude());
        }
    }
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
        // onConnectionFailed.
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
    }


    @Override
    public void onConnectionSuspended(int cause) {
        // The connection to Google Play services was lost for some reason. We call connect() to
        // attempt to re-establish the connection.
        Log.i(TAG, "Connection suspended");
        mGoogleApiClient.connect();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        buildGoogleApiClient();

        //////////////////////////////If activity was started with a bundle
        if (getIntent().getExtras() != null) {
            bundle = getIntent().getExtras().getBundle("bundle");
            if (bundle != null) {
                int size = bundle.getInt("size", 0);
                Log.d("MyLogs", "From Intent - " + bundle.getString("string"));
                array = new String[size];
                array = bundle.getStringArray("array");
                String hashtag = array[1];
                if (hashtag.indexOf("/") != -1) {
                    String[] parts = hashtag.split("/");
                    if (parts[1] != null) {
                        String part2 = parts[1];       //tracking_id
                        Map<String, Object> event = new HashMap<String, Object>();
                                Log.i(TAG,"last location"+ mLastLocation);                                          // logs null

                        event.put("sticker_ID", part2);
                        event.put("device_ID",android_id);
                        event.put("Latitude",mLastLocation);
                        event.put("longitude",mLongitude);
                        // Adding it to tracking system
                        KeenClient.client().queueEvent("android-sample-button-clicks", event);
                    }
                }
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }


    CountDownTimer CountDownTimer = new CountDownTimer(5000, 1000) {

        public void onTick(long millisUntilFinished) {
            if (millisUntilFinished / 1000 <= 3) {
                int time = (int) (millisUntilFinished / 1000);
                if (time == 2) {

                    proImage.setImageResource(R.drawable.ic_two);
                } else if (time == 1) {
                    proImage.setImageResource(R.drawable.ic_one);
                }
            }
        }

        public void onFinish() {
            proImage.setVisibility(View.GONE);
            takePicture();
        }
    };

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        //other stuff {
            Intent intent = new Intent(this, ImagePreview.class);
            if (getIntent().getExtras() != null) {
                if (bundle != null)
                    intent.putExtra("bundle", bundle);
            }
            startActivity(intent);
            finish();
        }
    }

}

另外,我的清单包括

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

我拿出了很多东西,但这里基本上是我处理位置的方式。我构建了一个googleApiClient,将lat和lon分配给mLastLocation的部分内容。我将它们发送到我的跟踪活动中。我的活动本质上只有5秒才跳进下一个活动(参见onPictureTaken)。它是一个倒计时。

我认为发生的事情是它无法在5秒内解析位置。应该5秒就够了吗?这看起来可能是问题吗?如果不是,即使下一个活动已经开始,我还有什么方法可以继续这项任务吗?然后我可以通过一些回调(在mLastLocation成功收到)上将数据发送到跟踪器上的下一个活动?

1 个答案:

答案 0 :(得分:1)

Activity lifecyle表示调用onCreate(),然后调用onStart()。在onStart(),您拨打mGoogleApiClient.connect(),然后在连接后异步调用onConnected()

因此,onCreate()永远不会有有效的位置,因为只有在调用onConnected()之后才能使用该位置(在onCreate()完成之后)。

您应该在获得有效位置后移动需要位置的代码。