2016-07-28 15 views
2

Ich benutze GoogleApiClient für die Überwachung des Standorts auf einem Dienst, der beim Booten beginnt, durch einen BroadcastReceiver, der auf android.intent.action.BOOT_COMPLETED hört.GoogleApiClient nicht verbunden am Dienst beim Booten gestartet

@Override 
    public void onReceive(Context context, Intent intent) { 
     Intent serviceA = new Intent(context, ServiceA.class); 
     startWakefulService(context, serviceA); 
    } 

Auf dem Service, den ich verwenden:

mGoogleApiClient = new GoogleApiClient.Builder(ServiceB.this) 
     .addConnectionCallbacks(mConnectionCallbacks) 
     .addOnConnectionFailedListener(mOnConnectionFailedListener) 
     .addApi(LocationServices.API).build(); 
    mGoogleApiClient.connect(); 

Der Dienst startet beim Booten, aber mein Problem ist, dass weder mConnectionCallbacks noch mOnConnectionFailedListener jemals genannt.

Ist etwas falsch mit dem, was ich mache. Diese Art des Aufrufs von GoogleApiClient funktioniert gut, wenn ich sie für Aktivitäten oder für Dienste verwende, die von Aktivitäten gestartet wurden.

Danke

Antwort

0
import com.google.android.gms.common.ConnectionResult; 

import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationServices; 

public class YourActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

protected GoogleApiClient mGoogleApiClient; 

@Override 
public void onCreate() { 
mGoogleApiClient = new GoogleApiClient.Builder(this) 
.addConnectionCallbacks(this) 
.addOnConnectionFailedListener(this) 
.addApi(LocationServices.API) 
.build(); 
} 


@Override 
protected void onStart() { 
super.onStart(); 


ConnectivityManager cm = 
(ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); 

NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
boolean isConnected = activeNetwork != null && 
activeNetwork.isConnectedOrConnecting(); 


if (isConnected) { 


mGoogleApiClient.connect(); 

} 

} 


/** 
* Runs when a GoogleApiClient object successfully connects. 
*/ 
@Override 
public void onConnected(Bundle connectionHint) { 
//do your works here. it is connected now 
} 


@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. 


    mGoogleApiClient.connect(); 
} 


@Override 
public void onConnectionFailed(ConnectionResult result) { 
    // Refer to the javadoc for ConnectionResult to see what error codes might be returned in 
    // onConnectionFailed. 


} 
+0

Sind Methoden zur ortsbezogene abhängig auch auf Network? Würde das offline funktionieren? – buzoherbert

+0

@buzoherbert diese Methode wird offiziell von Android hier empfohlen https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample Und es basiert auf Google Playservice. Ich habe Netzwerkinformationen nur verwendet, um zu überprüfen, ob die Internetverbindung aktiv ist oder nicht. du kannst es ignorieren ... danke –

+0

Danke, aber dann geht das nicht auf meine Frage ein. Ich habe die gleiche Implementierung, aber auf einen Dienst, der beim Booten beginnt. Und das ist, wo es nicht funktioniert. – buzoherbert