android - GoogleApiClient not connected on service started on boot -
i using googleapiclient listening location on service starts on boot, through broadcastreceiver listens android.intent.action.boot_completed .
@override public void onreceive(context context, intent intent) { intent servicea = new intent(context, servicea.class); startwakefulservice(context, servicea); }
on service use:
mgoogleapiclient = new googleapiclient.builder(serviceb.this) .addconnectioncallbacks(mconnectioncallbacks) .addonconnectionfailedlistener(monconnectionfailedlistener) .addapi(locationservices.api).build(); mgoogleapiclient.connect();
the service starts on boot, problem neither mconnectioncallbacks nor monconnectionfailedlistener ever called.
is there wrong i'm doing. way call googleapiclient works when use on activities or on services started activities.
thank you
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 googleapiclient object connects. */ @override public void onconnected(bundle connectionhint) { //do works here. connected } @override public void onconnectionsuspended(int cause) { // connection google play services lost reason. call connect() // attempt re-establish connection. mgoogleapiclient.connect(); } @override public void onconnectionfailed(connectionresult result) { // refer javadoc connectionresult see error codes might returned in // onconnectionfailed. }
Comments
Post a Comment