首先,在AndroidManifest.xml文件中添加android:sharedUserId属性,使两个应用共享相同的User ID:
在生产者应用中,创建并填充AHardwareBuffer,并通过binder传递给消费者应用:
// 生产者应用 // ...
// 创建AHardwareBuffer AHardwareBuffer_Desc desc = { .width = 128, .height = 128, .layers = 1, .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, .usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE, .stride = 0 }; AHardwareBuffer* ahb; AHardwareBuffer_allocate(&desc, &ahb);
// 填充AHardwareBuffer void* buffer; AHardwareBuffer_lock(ahb, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, NULL, &buffer); memset(buffer, 0xFF, 128 * 128 * 4); AHardwareBuffer_unlock(ahb, NULL);
// 通过binder传递AHardwareBuffer给消费者应用 JNIEnv* env; jvm->AttachCurrentThread(&env, NULL);
jclass class_Binder = env->FindClass("android/os/Binder"); jmethodID method_getService = env->GetStaticMethodID(class_Binder, "getService", "(Ljava/lang/String;)Landroid/os/IBinder;"); jobject service = env->CallStaticObjectMethod(class_Binder, method_getService, env->NewStringUTF("MyService"));
jclass class_IBinder = env->FindClass("android/os/IBinder"); jmethodID method_asInterface = env->GetMethodID(class_IBinder, "asInterface", "(Landroid/os/IBinder;)Landroid/os/IInterface;"); jobject myInterface = env->CallObjectMethod(service, method_asInterface, service);
jclass class_MyInterface = env->FindClass("com/example/MyInterface"); jmethodID method_transfer = env->GetMethodID(class_MyInterface, "transfer", "(Landroid/hardware/HardwareBuffer;)V"); jobject hardwareBuffer = AHardwareBuffer_toHardwareBuffer(env, ahb); env->CallVoidMethod(myInterface, method_transfer, hardwareBuffer);
// ...
在消费者应用中,接收并使用收到的AHardwareBuffer:
// 消费
上一篇:AndroidNDK/JNI--sharedlibraryincompatiblewitharmelf_linux_eabi
下一篇:AndroidNDK:Handlingandcreatinginputeventsinsideandroid_mainfunction