0

我正在关注这个: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 时)?

4

1 回答 1

1

Activity 生命周期显示被onCreate()调用,然后onStart()被调用。在您的onStart()中,您调用mGoogleApiClient.connect(),然后onConnected()在连接后异步调用。

因此,您将永远不会有一个有效的位置,onCreate()因为在调用 after 之前它不可用onConnected()(这是在onCreate()完成之后)。

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

于 2015-02-12T03:51:21.983 回答