How to access Package Name from JNI/NDK?
#include <jni.h>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/file.h>

#include <dirent.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>

extern "C"
jstring
Java_xl_com_myapplication_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";

    //getuid

    char args[4096], path[4096];

    pid_t pid = getpid();

    snprintf(path, sizeof(path), "/proc/%u/cmdline", pid);
    int fd = open(path, O_RDONLY);
    if (fd < 0) {
        return NULL;
    }
    ssize_t len = read(fd, args, sizeof(args));

    close(fd);
    if (len < 0 || len == sizeof(args)) {
        return NULL;
    }

    printf("The package name is %s\n", args);

    hello = args;
    return env->NewStringUTF(hello.c_str());
}

http://man7.org/linux/man-pages/man2/getpid.2.html
https://android.googlesource.com/platform/system/core.git/+/donut-release/toolbox/ps.c

阅读更多
文章标签: c语言 android
个人分类: android开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

How to access Package Name from JNI/NDK?

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭