Ciao a tutti,
sto sviluppando, per mio diletto, un'app antifurto, ovvero di rintracciabilità delle coordinate del telefono tramite una "password" inviata da un mio server php, tramite il servizio di google GCM. Ho creato una main activity principale che si occupa della registrazione Id per i servizi Google, un GCM broacast receiver e un service, chiamato "rintraccia.java" che si occupa di recuperare le coordinate GPS del telefono, sempre tramite le Google Api per la localizzazione.
Il problema è che non mi funziona la parte rintraccia.java, ovvero all'arrivo di un messaggio determinato (contenente la parola "password"), si avvia il service rintraccia.java ma poco dopo mi va in crash e android studio mi restituisce questo errore:
codice:
06-07 15:53:37.081 6710-7691/xxxxxxxxxx.com.gcmsimplex E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[rintraccia]
Process: xxxxxxxxxxx.com.gcmsimplex, PID: 6710
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
at com.google.android.gms.common.api.zze.zzb(Unknown Source)
at com.google.android.gms.common.api.zzg.zzb(Unknown Source)
at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
at xxxxxxxxxxxx.com.gcmsimplex.rintraccia.onHandleIntent(rintraccia.java:91)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.os.HandlerThread.run(HandlerThread.java:61)
Quindi mi dice che il GoogleApiClient non è ancora connesso...io però ho messo il comando mGoogleApiClient.connect();
Qui la parte della main activity che si occupa di cercare la password nel messaggio e di avviare il service se la password è corretta:
codice:
public void controlla(){
String password = "password";
if (mes.equals(password)) {
startService();
stopService();
}
else {
//visualizza altri messaggi;
}
}
public void startService()
{
startService(new Intent(this,rintraccia.class));
}
public void stopService()
{
stopService(new Intent(this,rintraccia.class));
}
Mentre qui c'è rintraccia.java
codice:
public class rintraccia extends IntentService implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
public rintraccia()
{
super("rintraccia");
}
// private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private void handleNewLocation(Location location) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
Toast toast=Toast.makeText(this,(Double.toString(location.getLatitude())) + " , " + (Double.toString(location.getLongitude())), Toast.LENGTH_LONG);
toast.show();
}
@Override
public void onConnected(Bundle connectionHint) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
else {
handleNewLocation(location);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast toast=Toast.makeText(this,"Errore nella connessione!",Toast.LENGTH_LONG);
toast.show();
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
}
@Override
protected void onHandleIntent(Intent arg0) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(600000) // 10 seconds, in milliseconds
.setFastestInterval(0); // 1 second, in milliseconds
mGoogleApiClient.connect();
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
handleNewLocation(location);
}
}
@Override
public void onDestroy()
{
Toast toast=Toast.makeText(this,"Coordinate tracciate",Toast.LENGTH_LONG);
toast.show();
}
}
Mi potete aiutare a capire dove sbaglio?
grazie mille!!