From f4780aa54b72fdc00d2a3d43b9298aad0a04d34e Mon Sep 17 00:00:00 2001 From: houdisheng Date: Wed, 14 Dec 2022 15:21:32 +0800 Subject: [PATCH] =?UTF-8?q?appstateobserver=E7=B1=BB=E5=9E=8B=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: houdisheng --- services/common/include/bundle_active_service.h | 2 +- services/common/src/bundle_active_service.cpp | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index f0e5dd3..5f186a5 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -211,7 +211,7 @@ protected: private: std::shared_ptr bundleActiveCore_; std::shared_ptr reportHandler_; - std::shared_ptr appStateObserver_; + sptr appStateObserver_; #ifdef BGTASKMGR_ENABLE std::shared_ptr continuousTaskObserver_; #endif diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 393d9f8..67b08f9 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -167,8 +167,12 @@ OHOS::sptr BundleActiveService::GetAppManagerInstance void BundleActiveService::InitAppStateSubscriber(const std::shared_ptr& reportHandler) { - if (appStateObserver_ == nullptr) { - appStateObserver_ = std::make_shared(); + if (!appStateObserver_) { + appStateObserver_ = new (std::nothrow)BundleActiveAppStateObserver(); + if (!appStateObserver_) { + BUNDLE_ACTIVE_LOGE("malloc app state observer failed"); + return; + } appStateObserver_->Init(reportHandler); } } @@ -190,7 +194,7 @@ bool BundleActiveService::SubscribeAppState() BUNDLE_ACTIVE_LOGE("SubscribeAppState appstateobserver is null, return"); return false; } - int32_t err = appManager->RegisterApplicationStateObserver(appStateObserver_.get()); + int32_t err = appManager->RegisterApplicationStateObserver(appStateObserver_); if (err != 0) { BUNDLE_ACTIVE_LOGE("RegisterApplicationStateObserver failed. err:%{public}d", err); return false; -- Gitee