私はこれに従っています: 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 を作成し、緯度と経度を mLastLocation の一部に割り当てます。そして、それらを追跡イベントに送信します。私のアクティビティは本来、次のアクティビティにジャンプするまでに 5 秒しかありません (onPictureTaken を参照)。カウントダウンです。
何が起こっているのかは、5 秒で場所を解決できないということだと思います。5秒で十分ですか?これが問題のように思えますか?そうでない場合、次のアクティビティが開始されても、このタスクを続行する方法はありますか? 次に、コールバックを使用して次のアクティビティでデータをトラッカーに送信できますか (mLastLocation で正常に受信された場合)?