Search code examples
androidaidl

Android checking background service availability?


I built an Android Launcher for my company billboard, that exposes an AIDL service like below:

interface IBillboardAPI {

    String onAppStart(String mediaKey, String session);
    String onAppSleep(String mediaKey, String session);
    String getAppCampaignsList(String mediaKey, String session);
    String getAppCampaign(String mediaKey, String session, long campaignId);
    byte[] downloadCampaignMaterial(String url);

}

And I also built a java lib that will integrate any Game (which using this lib) with our Android Launcher if the game installed within our billboard.

I can connect the game and the AIDL service through my lib, using the code below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.info(">>> ON CREATE...");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.info(">>> CONNECTED...");
            billboardAPI = IBillboardAPI.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            billboardAPI = null;
            Log.info(">>> DISCONNECTED...");
        }
    };

    if (billboardAPI == null) {
        Log.info(">>> CONNECTING TO SERVICE...");
        Intent i = new Intent();
        i.setAction("service.BILLBOARD_API");
        bindService(i, connection, Service.BIND_AUTO_CREATE);
    }
}

Since the Game will not only be installed within our billboard, but also at user's mobile... I wonder,

HOW can I make my lib throw an event when the connection code above, failed to connect with our billboard AIDL service because the Game is not installed within our billboard but user's phone?

There seems no METHOD available in ServiceConnection to notify the activity that the service it is trying to connect is not available.

Thanks.


Solution

  • Per the bindService() documentation, the boolean value it returns is

    If you have successfully bound to the service, true is returned; false is returned if the connection is not made so you will not receive the service object.

    If the service does not exist, then bindService will return false.