obsidian/笔记文件/2.笔记/Android 的 JNI签名Notification.md
2025-03-26 00:02:56 +08:00

1.4 KiB
Raw Permalink Blame History

#安卓

Android 的 JNIJava Native Interface签名中Landroid/app/Notification; 表示 android.app.Notification 类。这种表示方法用于 JNI 签名中,以标识一个具体的 Java 类。

在 JNI 中,如果需要调用与 Notification 相关的 Java 方法或字段,可以使用 Landroid/app/Notification; 来指代该类。例如,当使用 GetMethodIDGetFieldID 等 JNI 函数时,你需要传递方法或字段的完整签名,包括参数类型和返回类型,这时就用到了这种签名表示方式。

示例:获取 Notification 类的方法 ID

假设我们要在 native 层获取 Notificationnotify 方法的 ID可以这样写

// 获取 Notification 类引用
jclass notificationClass = env->FindClass("android/app/Notification");

// 获取 notify 方法的 ID
// 假设 notify 方法的签名是void notify(int id, Notification notification)
jmethodID notifyMethodID = env->GetMethodID(notificationClass, "notify", "(ILandroid/app/Notification;)V");

在这里:

  • Landroid/app/Notification; 表示 Notification 类的对象类型。
  • (ILandroid/app/Notification;)Vnotify 方法的 JNI 签名,其中 I 表示 int 参数,Landroid/app/Notification; 表示 Notification 对象,V 表示 void 返回类型。

这样JNI 签名可以帮助我们在 native 层与 Java 层之间准确传递参数和返回值。