diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index c0ec21705dda6664982e55965135d2de37b46d7f..64732cd3c9821fb31fe4ed3e05aab679850bd786 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -66,6 +66,7 @@ private: std::shared_ptr handler_; int ConvertIntervalType(const int intervalType); void InitNecessaryState(); + void InitService(); bool GetBundleMgrProxy(); bool CheckBundleIsSystemAppAndHasPermission(const int uid, const int userId); void InitAppStateSubscriber(const std::shared_ptr& reportHandler); diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 5673785421e299a36ff49797cf9b9bf2ae609f5e..652450d2660e25558d3dded3810a6e88302fb732 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -75,6 +75,24 @@ void BundleActiveService::InitNecessaryState() return; } + if (systemAbilityManager == nullptr + || systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) == nullptr + || systemAbilityManager->GetSystemAbility(POWER_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(COMMON_EVENT_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(BACKGROUND_TASK_MANAGER_SERVICE_ID) == nullptr + || systemAbilityManager->GetSystemAbility(TIME_SERVICE_ID) == nullptr) { + BUNDLE_ACTIVE_LOGI("request system service object is not ready yet!"); + auto task = [this]() { this->InitNecessaryState(); }; + handler_->PostTask(task, DELAY_TIME); + return; + } + + InitService(); +} + +void BundleActiveService::InitService() +{ if (bundleActiveCore_ == nullptr) { bundleActiveCore_ = std::make_shared(); bundleActiveCore_->Init(); @@ -118,6 +136,9 @@ OHOS::sptr BundleActiveService::GetAppManagerInstance OHOS::sptr systemAbilityManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); OHOS::sptr object = systemAbilityManager->GetSystemAbility(OHOS::APP_MGR_SERVICE_ID); + if (!object) { + return nullptr; + } return OHOS::iface_cast(object); } @@ -141,7 +162,7 @@ bool BundleActiveService::SubscribeAppState() { BUNDLE_ACTIVE_LOGI("SubscribeAppState called"); sptr appManager = GetAppManagerInstance(); - if (appStateObserver_ == nullptr) { + if (appStateObserver_ == nullptr || appManager == nullptr) { BUNDLE_ACTIVE_LOGE("SubscribeAppState appstateobserver is null, return"); return false; }