In Android >= 10,How to bind to a Service in another Android app that has not been started yet?

41 Views Asked by At

Question:I have an Android app that needs to call a Service provided by another app, but when the other app is killed, bindService() returns false. How can I achieve this on Android 10 or later?

private boolean bindService() {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(ConfigCons.MYKEY_PACKAGE_NAME, ConfigCons.CONNECT_CLASS_PATH));
            context.startService(intent);
            boolean bindResult = context.bindService(intent, connection, Context.BIND_AUTO_CREATE);
            if (!bindResult) {
                LogUtil.e(TAG, "can not bind connect service");
            }
            return bindResult;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
}
0

There are 0 best solutions below