通过jni获得应用Icon(getApplicationIcon)

jni获得应用icon有两步

1.在java端写一个方法,通过包名获取appIcon,返回byte[]

native初始化时,获取这个method,同时拿到VM和全局context

jobject mBzAppContext;
JavaVM *m_appContext_VM = NULL;

mAppContext = env->NewGlobalRef(thiz);
env->GetJavaVM(&m_appContext_VM);

FIND_CLASS(clazz, "com/xx/xx/xx");
if (clazz == NULL) {
loge("class not exist!!");
}

GET_METHOD_ID(gAppGlobalCallMethod.getAppIcon, clazz,
"getAppIcon", "(Ljava/lang/String;)[B");
if (gAppGlobalCallMethod.getAppIcon == NULL) {
loge("method get fail!!");
}

(Ljava/lang/String;)[B 指入参是String,回参是jbyteArray

2.调用jni函数,拿到byte[],转成char*

void readAppIcon(const char* packageName)
{
if (m_appContext_VM == NULL) {
loge("m_appContext_VM null");
return;
}
if (mAppContext == NULL) {
loge("mAppContext null");
return;
}
if (gAppGlobalCallMethod.getAppIcon== NULL) {
loge("gAppGlobalCallMethod.getAppIcon null");
return;
}
CHECK_JVM_THREAD(m_appContext_VM);
if (error) {
loge("check jvm thread error");
return;
}
jstring jPackageName = env->NewStringUTF(packageName);
jbyteArray jbarray = (jbyteArray)env->CallObjectMethod(mBzAppContext, gAppGlobalCallMethod.getAppIcon, jPackageName);

if (jbarray == nullptr) {
logi("getAppIcon null");
} else {
len = (int)env->GetArrayLength (jbarray);
char *data = (char*)env->GetByteArrayElements(jbarray, 0);
// 这里就把appicon拿到并转成了char*,注意释放data

env->ReleaseByteArrayElements(jbarray, data, 0);
}
if (attached) {
m_appContext_VM->DetachCurrentThread();
}
}

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注