1

我正在获取当前位置的纬度和经度,但onLocationChanged在我正在使用PRIORITY_BALANCED_POWER_ACCURACY模式的移动数据上返回相同的位置。

请告诉我哪里行为不端这是我的代码

public class Latlng_Service extends Service implements
        com.google.android.gms.location.LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {


    public static String STOP_USER_SERVICE = "STOP_USER_SERVICE";
    private static final long INTERVAL = 3000 * 10;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    private WakeLock wl;


    int distance = 0;
    List<Address> address_list;
    String add_String,cat_id,module_id = "3";
    LocationRequest locationRequest;
    GoogleApiClient googleApiClient;
    Location mloLocation, lastlocation;
    String lat, lng, accuracy, name;
    String battery_level;
    String signal_strength;
    Handler  fivemiuntesHandler,thirtysecHandler;
    Start_latlng_service start_latlng_service;
    String login_id, gcm_id, url, response,responseArray[];
    boolean flag = false;

    List<LatLng> lisLatLngs = new ArrayList<LatLng>();
    // public static final String BROADCAST_ACTION =
    // "com.xsinfosol.smart_crm.attndance.location";
    Intent intent;

    String count;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        createLocationRequest();
        flag = false;
        googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API).addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();

        googleApiClient.connect();

        start_latlng_service = new Start_latlng_service();
        this.registerReceiver(this.broadcastReceiver, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
        this.registerReceiver(this.stop_service_at_time, new IntentFilter(
                STOP_USER_SERVICE));


    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub



        PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "whatever");
            wl.acquire();

        return START_STICKY;
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        mloLocation = location;
        lat = String.valueOf(location.getLatitude());
        lng = String.valueOf(location.getLongitude());
        accuracy = String.valueOf(location.getAccuracy());
        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

        myConnection.logs_onlocation_insert(currentDateTimeString, String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));



        AtendenceAutoManualFunction(location);

    }

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

        System.out.println("Connection Falied " + arg0.getErrorCode());
    }

    @Override
    public void onConnected(Bundle arg0) {
        // TODO Auto-generated method stub
        startLocationUpdates();
        this.registerReceiver(this.broadcastReceiver, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
    }

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

    }

    protected void createLocationRequest() {
        locationRequest = new LocationRequest();

        locationRequest
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        locationRequest.setInterval(INTERVAL).setFastestInterval(
                FASTEST_INTERVAL);
        // .setSmallestDisplacement(10);
        // .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    }

    protected void startLocationUpdates() {
        @SuppressWarnings("unused")
        PendingResult<Status> pendingResult = LocationServices.FusedLocationApi
                .requestLocationUpdates(googleApiClient, locationRequest, this);
        Log.d(TAG, "Location update started ..............: ");

        if (fivemiuntesHandler == null) {

            Log.d(TAG, "Location update started five minutes ..............: ");
            fivemiuntesHandler = new Handler();

            fivemiuntesHandler.postDelayed(fiveminutes, 300000);

        }

        if (thirtysecHandler == null) {
            Log.d(TAG, "Location update started five minutes ..............: "); 
            thirtysecHandler = new Handler();
            thirtysecHandler.postDelayed(thirtySec, 0);

        }

    }

    protected void stopLocationUpdates() {
        if (googleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(
                    googleApiClient, this);
        }
        Log.d(TAG, "Location update stopped .......................");
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        stopLocationUpdates();

        googleApiClient.disconnect();

        unregisterReceiver(broadcastReceiver);
        unregisterReceiver(stop_service_at_time);
        sharedPreferences = getSharedPreferences("user_detail",
                MODE_WORLD_WRITEABLE);
        login_id = sharedPreferences.getString("login_id", "");
        gcm_id = sharedPreferences.getString("reg_id", "");

        if (!login_id.contentEquals("") && !gcm_id.contentEquals("")
                && flag == false) {
            sendBroadcast(new Intent("CantKillMe"));

        } else {
            wl.release();
            stopSelf();
        }
    }
}
4

0 回答 0