无法解决JNI功能:Android ndk

时间:2016-11-18 07:07:57

标签: java android c++ android-ndk java-native-interface

我希望通过从C ++代码调用Android方法来获取Android中安装的应用程序列表。找到第一个方法(getPackageManager()),但每次getInstalledApplications()方法都是零。这有什么不对?

JNIEXPORT jobject JNICALL Java_com_example_davidt_ndkexample_TestNdk_getListOfInstalledApps
    (JNIEnv* env, jobject obj, jobject activity){
     jclass contextWrapperClass = env->FindClass("android/content/ContextWrapper");
     jmethodID getPackageManagerId = env->GetMethodID(contextWrapperClass,"getPackageManager","()Landroid/content/pm/PackageManager;");
     if (getPackageManagerId == 0) {
         fprintf(stderr, "packageManager() not found");
     }

     jobject pm = env->CallObjectMethod(activity, getPackageManagerId);

     jclass PackageManagerClass = env->GetObjectClass(pm);
     jmethodID getInstalledApplicationsId = env->GetMethodID(PackageManagerClass,"getInstalledApplications","(I)[Landroid/content/pm/ApplicationInfo;");
     if (getInstalledApplicationsId == 0) {
        fprintf(stderr, "getInstalledApplications() not found");
     }

     return env->CallObjectMethod(pm, getInstalledApplicationsId);
 }

1 个答案:

答案 0 :(得分:1)

看起来你的签名描述符是错误的。我使用了javap工具来检查它

javap -s -bootclasspath <Your SDK Installation Path>/platforms/android-25/android.jar android.content.pm.PackageManager

并且对于getInstalledApplications,它返回

public abstract java.util.List<android.content.pm.ApplicationInfo> getInstalledApplications(int);
    descriptor: (I)Ljava/util/List;

所以,在你的情况下,它必须是

jmethodID getInstalledApplicationsId = env->GetMethodID(PackageManagerClass,"getInstalledApplications","(I)Ljava/util/List");