Android 检查后台服务可用性?

Android checking background service availability?

我为我的公司 billboard 构建了一个 Android Launcher,它公开了一个 AIDL 服务如下:

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);

}

而且我还构建了一个 java 库,如果游戏安装在我们的广告牌

我可以使用下面的代码通过我的库连接游戏和 AIDL 服务:

@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);
    }
}

因为游戏不仅会安装在我们的 广告牌 中,还会安装在用户的手机上...我想知道,

当上面的连接代码无法连接我们的广告牌 AIDL 服务时,我该如何让我的库抛出一个事件,因为游戏没有安装在我们的广告牌中,而是安装在用户的 phone 中?

ServiceConnection 中似乎没有可用的方法来通知 activity 它正在尝试连接的服务不可用。

谢谢。

根据 bindService() documentationboolean 的值 return 是

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.

如果服务不存在,那么bindService会returnfalse