【问题标题】:How to get FusedLocation from IntentService如何从 IntentService 获取 FusedLocation
【发布时间】:2015-07-07 05:03:42
【问题描述】:
  • 如何使用新的FusedLocation 检索当前位置 IntentService 的 API?
  • 我怎样才能确保 IntentService 将收到所有 来自 FusedLocation API回调? IE, 如何同步获取位置对象?

【问题讨论】:

  • 首先将IntentService 替换为常规服务。 IntentService 不处理调用本身是异步的 API,例如融合位置提供程序 API。您将需要一个常规服务,带有您自己的后台线程(如果需要),只有在您获得数据(或达到某个超时)时,您才能在其中关闭服务。 “如何同步获取位置对象?” -- 不能保证你可以,这就是为什么你用常规服务替换你的IntentService
  • @DroidHacker 这个问题你解决了吗?

标签: android google-play-services fusedlocationproviderapi android-fusedlocation


【解决方案1】:

这可能对你有帮助

import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.AlertDialog;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.IBinder;
    import android.util.Log;
    import android.widget.Toast;

    import com.example.driverapplication.CommonUtilities;
    import com.example.driverapplication.R;
    import com.example.driverapplication.ServiceHandler;
    import com.example.driverapplication.utilities.ConnectionDetector;
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.location.LocationServices;

    public class LocationUpdate extends Service implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {
        private static final String TAG = "DRIVER";
        private LocationManager mLocationManager = null;
        private static final int LOCATION_INTERVAL = 30000;
        private static final float LOCATION_DISTANCE = 0;
        private double currentLat, currentLng;
        private SharedPreferences pref;
        private String driverId;
        private GoogleApiClient mGoogleApiClient;
        // A request to connect to Location Services
        private LocationRequest mLocationRequest;

        private LocationListener locationListener;

        private class LocationListener implements
                com.google.android.gms.location.LocationListener {

            public LocationListener() {
            }

            @Override
            public void onLocationChanged(Location location) {
                Log.e(TAG, "onLocationChanged: " + location);
                currentLat = location.getLatitude();
                currentLng = location.getLongitude();

            }


        }


        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.e(TAG, "onStartCommand");
            super.onStartCommand(intent, flags, startId);
            boolean stopService = false;
            if (intent != null)
                stopService = intent.getBooleanExtra("stopservice", false);

            System.out.println("stopservice " + stopService);

            locationListener = new LocationListener();
            if (stopService)
                stopLocationUpdates();
            else {
                if (!mGoogleApiClient.isConnected())
                    mGoogleApiClient.connect();
            }

            return START_STICKY;
        }

        @Override
        public void onCreate() {
            Log.e(TAG, "onCreate");
            pref = getSharedPreferences("driver_app", MODE_PRIVATE);
            driverId = pref.getString("driver_id", "");
            mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
        }



        @Override
        public void onDestroy() {
            Log.e(TAG, "onDestroy");
            super.onDestroy();
        }

        public void stopLocationUpdates() {
            LocationServices.FusedLocationApi.removeLocationUpdates(
                    mGoogleApiClient, locationListener);

            if (mGoogleApiClient.isConnected())
                mGoogleApiClient.disconnect();
        }


        @Override
        public void onConnectionFailed(ConnectionResult arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onConnected(Bundle arg0) {
            // TODO Auto-generated method stub
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            mLocationRequest.setInterval(35000);
            mLocationRequest.setFastestInterval(30000);
            startLocationUpates();
        }
        private void startLocationUpates() {
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, locationListener);
        }


        @Override
        public void onConnectionSuspended(int arg0) {
            // TODO Auto-generated method stub

        }

    }

【讨论】:

  • 请注意,Service(相对于 IntentService)在应用程序的主线程中工作。如果不在此服务中创建自己的线程,您将阻塞应用程序 UI 线程,这可能会导致糟糕的用户体验。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
相关资源
最近更新 更多