user2610533 user2610533 - 3 months ago 14
Android Question

Android service asyncrounous: How to make sure that connection is established before running onStartCommand

I have class what creates connection to google play service, when connection is established

onConnectedToGooglePlayService()
is called. When new
mySerivce
is started, first thing is to establish connection to play service as you can see in the code.

The problem is that connecting to play service takes some time and I can't be sure that connection is established when
onStartCommand
is called and I can't get info from Intent.

So my question is: What is the best way to handle this situation? I thought of creating a global variable and processing intent when connection is established, but I am not sure that's the right way to do this.

public class myService extends Service implements ConnectedToGooglePlayService {
GoogleApiClient mGoogleApiClient;

@Override
public void onCreate() {
super.onCreate();
new ConnectToGooglePlayService(this, this).connect();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onConnectedToGooglePlayService(GoogleApiClient mGoogleApiClient) {
this.mGoogleApiClient = mGoogleApiClient;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

}

Answer

You can let know your Activity or Fragments when to start the service by using an Interface. See the code below.

public class myService extends Service implements ConnectedToGooglePlayService {
GoogleApiClient mGoogleApiClient;

public interface OnGPlayServiceConnected {
void googlePlayServiceConnected();
}
private OnGPlayServiceConnected gplayInterface;

public myService(OnGPlayServiceConnected gplayInterface){
        this.gplayInterface = gplayInterface;
}

@Override
public void onCreate() {
        super.onCreate();
        new ConnectToGooglePlayService(this, this).connect();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
}

@Override
public void onConnectedToGooglePlayService(GoogleApiClient mGoogleApiClient) {
        this.mGoogleApiClient = mGoogleApiClient;
        // Call the interface whenever you're connected to Play Service
        gplayInterface.googlePlayServiceConnected();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
        return null;
}

}

Now that we have our interface ready, we should implement this interface right where you want to call your service.

public class SomeActivity extends Activity implements OnGPlayServiceConnected {

private myService yourService;

@Override
public void onCreate(Bundle savedInstanceState){
        // Other codes

        yourService = new myService(this);
}

@Override
public void googlePlayServiceConnected {
//Now start your service from here
        yourService.start();
}

}

Basically this interface is working like a listener. When you'll call it from onConnectedToGooglePlayService, it will call this SomeActivity class's googlePlayServiceConnected method. So, now you know the connecting process has come to its end and you're good to go starting your service.

Comments