Java サービスにアクセスするためにネイティブ サービスをカスタマイズする

C++ は Java ヘッダー ファイルをインポートできないため、C++ サービスが Java サービスにアクセスするのは少し面倒です。

ネイティブ サービスはクライアントとして機能し、そのコードは次のディレクトリにあります。

android/frameworks/native/services/customizemanagerservice

WMS はサーバー側として機能し、そのコード パスは次のとおりです。

android/frameworks/base/services/core/java/com/android/server/wm

1. Frameworks/base/core/java/android/view/IWindowManager.aidl にカスタム インターフェイスを追加します。

+ /** 
+ * カスタマイズを設定します。
+ */ 
+ int setcustomize();

2. 対応する実装を Frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java に追加します。

+
+ @Override
+ public int setcustomize() { + Slog.d(TAG, "setcustomize"); + 1 を返します。+ }


3. Frameworks/native/services/customizemanagerservice の下に次のファイルを作成します

IWindowManager.cpp*

ウィンドウマネージャー.cpp*

include_windowmanager/IWindowManager.h

include_windowmanager/WindowManager.h

include_windowmanager/IWindowManager.h
#ifndef ANDROID_IWINDOW_MANAGER_H
#define ANDROID_IWINDOW_MANAGER_H

#ifndef __ANDROID_VNDK__

#include <binder/IInterface.h>

namespace android {

// ------------------------------------------------------------------------------------

class IWindowManager : public IInterface
{
public:
    DECLARE_META_INTERFACE(WindowManager)

    virtual int setcustomize() = 0;

    enum {
        CHECK_IF_WEARING = IBinder::FIRST_CALL_TRANSACTION,
    };
};

// ------------------------------------------------------------------------------------

} // namespace android

#else // __ANDROID_VNDK__
#error "This header is not visible to vendors"
#endif // __ANDROID_VNDK__

#endif // ANDROID_IACTIVITY_MANAGER_H
IWindowManager.cpp*

#include <WindowManager.h>
#include <IWindowManager.h>
#include <binder/Parcel.h>
#include <utils/Errors.h>

namespace android {

// ------------------------------------------------------------------------------------

class BpWindowManager : public BpInterface<IWindowManager>
{
public:
    explicit BpWindowManager(const sp<IBinder>& impl)
        : BpInterface<IWindowManager>(impl)
    {
    }

    virtual int setcustomize()
    {
        Parcel data, reply;
        data.writeInterfaceToken(IWindowManager::getInterfaceDescriptor());
        status_t ret = remote()->transact(SET_CUSTOMIZE, data, & reply);
        if (ret == NO_ERROR) {
            return reply.readInt32();
        }
        return -1;
    }
};

// ------------------------------------------------------------------------------------

IMPLEMENT_META_INTERFACE(WindowManager, "android.view.IWindowManager")

} // namespace android
include_windowmanager/WindowManager.h
#pragma once

#ifndef __ANDROID_VNDK__

#include <IWindowManager.h>

#include <utils/threads.h>

// ---------------------------------------------------------------------------
namespace android {

class WindowManager
{
public:
    WindowManager();
    int setcustomize();
    status_t linkToDeath(const sp<IBinder::DeathRecipient>& recipient);
    status_t unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient);
private:
    Mutex mLock;
    sp<IWindowManager> mService;
    sp<IWindowManager> getService();
};


} // namespace android
// ---------------------------------------------------------------------------
#else // __ANDROID_VNDK__
#error "This header is not visible to vendors"
#endif // __ANDROID_VNDK__

 

WindowManager.cpp*
#include <mutex>
#include <unistd.h>

#include <WindowManager.h>
#include <binder/Binder.h>
#include <binder/IServiceManager.h>

#include <utils/SystemClock.h>

namespace android {

WindowManager::WindowManager()
{
}

sp<IWindowManager> WindowManager::getService()
{
    std::lock_guard<Mutex> scoped_lock(mLock);
    int64_t startTime = 0;
    sp<IWindowManager> service = mService;
    while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
        sp<IBinder> binder = defaultServiceManager()->checkService(String16("window"));
        if (binder == nullptr) {
            // Wait for the window service to come back...
            if (startTime == 0) {
                startTime = uptimeMillis();
                ALOGI("Waiting for window service");
            } else if ((uptimeMillis() - startTime) > 1000000) {
                ALOGW("Waiting too long for window service, giving up");
                service = nullptr;
                break;
            }
            usleep(25000);
        } else {
            service = interface_cast<IWindowManager>(binder);
            mService = service;
        }
    }
    return service;
}

int WindowManager::setcustomize()
{
    sp<IWindowManager> service = getService();
    return service != nullptr ? service->setcustomize() : -1;
}

status_t WindowManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
    sp<IWindowManager> service = getService();
    if (service != nullptr) {
        return IInterface::asBinder(service)->linkToDeath(recipient);
    }
    return INVALID_OPERATION;
}

status_t WindowManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
    sp<IWindowManager> service = getService();
    if (service != nullptr) {
        return IInterface::asBinder(service)->unlinkToDeath(recipient);
    }
    return INVALID_OPERATION;
}

} // namespace android
 
 

4.ネイティブサービスでJavaサービスを呼び出す

CustomizeManagerService.cpp
#include "WindowManager.h"
int CustomizeManagerService::setcustomize(){
    WindowManager wm;
    return wm.setcustomize();
}

おすすめ

転載: blog.csdn.net/weixin_41028555/article/details/131212658