Bonjour à tous

J'ai un problème avec l'API GoogleApiClient, particulièrement sur la partie localisation. Les coordonnées ne changent que environ toutes les 5min, alors que moi je veux qu'a chaque fois que je clique sur le bouton getLocation que je puisse avoir les nouvelles coordonnées sans attendre.

Voici la classe de mon activity

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
 
package com.pharma.app.app_pharma.main;
 
 
import android.app.ProgressDialog;
import android.location.Location;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
 
 
import java.util.*;
 
 
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.pharma.app.app_pharma.entity.Position;
import com.pharma.app.app_pharma.main.adapter.PharmacyAdapter;
import com.pharma.app.app_pharma.provider.PharmacyProvider;
import com.pharma.app.app_pharma.R;
import com.pharma.app.app_pharma.entity.Pharmacy;
 
 
 
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener  {
 
 
    private ListView listPharmacy;
    private PharmacyProvider _pharmacyProvider = new PharmacyProvider();
 
 
 
    private ProgressBar progress;
 
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    private LocationRequest mLocationRequest;
    private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        listPharmacy = (ListView) findViewById(R.id.listPharmacies);
        progress = (ProgressBar) findViewById(R.id.progress);
        progress.setVisibility(View.GONE);
        List<Pharmacy> _list = _pharmacyProvider.getPharmacies();
        listPharmacy.setAdapter(new PharmacyAdapter(MainActivity.this, R.layout.item_list, _list));
 
 
        if (checkPlayServices()) {
           // Building the GoogleApi client
            buildGoogleApiClient();
        }
    }
 
 
    public void search(View view) {
        Position _pos  =this.getLocation();
        if(_pos!=null){
            Toast.makeText(MainActivity.this,"lat = " +_pos._lat+" lon = "+ _pos._lon,Toast.LENGTH_LONG).show();
           // getJSON();
        }else {
            Toast.makeText(MainActivity.this,"Problème rencontré",Toast.LENGTH_LONG).show();
        }
    }
 
    public Position getLocation(){
 
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
 
        Position _position = new Position();
        if (mLastLocation != null) {
            _position._lat = mLastLocation.getLatitude();
            _position._lon = mLastLocation.getLongitude();
 
        } else {
 
        }
        return _position;
    }
 
    /**
     * Method to verify google play services on the device
     * */
    private boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Votre téléphone n'est pas supporté.", Toast.LENGTH_LONG)
                        .show();
                finish();
            }
            return false;
        }
        return true;
    }
 
    /**
     * Creating google api client object
     * */
    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();
    }
 
    @Override
    protected void onStart() {
        super.onStart();
        if (mGoogleApiClient != null) {
            mGoogleApiClient.connect();
        }
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        checkPlayServices();
    }
 
    @Override
    public void onConnected(Bundle bundle) {
 
    }
 
    @Override
    public void onConnectionSuspended(int i) {
 
    }
 
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
 
    }
 
    private void getJSON() {
        class GetJSON extends AsyncTask<String, Void, Pharmacy[]> {
            ProgressDialog loading;
 
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(MainActivity.this, "Please Wait...",null,true,true);
            }
 
            @Override
            protected  Pharmacy[] doInBackground(String... params) {
                return _pharmacyProvider.getPharmacies(1,0);
               /* String uri = params[0];
                BufferedReader bufferedReader = null;
                try {
                    URL url = new URL(uri);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();
 
                    bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
 
                    String json;
                    while((json = bufferedReader.readLine())!= null){
                        sb.append(json+"\n");
                    }
 
                    JSONArray jsonArray = new JSONArray(json);
 
                    return sb.toString().trim();
 
                }catch(Exception e){
                    return null;
                }*/
 
            }
 
            @Override
            protected void onPostExecute( Pharmacy[] s) {
                super.onPostExecute(s);
                loading.dismiss();
 
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();
    }
 
 
 
}

Bien à vous