一、NDK中获取android设备ID的方式

Java代码如下(获取设备ANDROID_ID):

final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);

对应的c代码(相当蛋疼),注意如果是C++,需要把所有的(*env)->替换成env->,同时去掉第一个参数env

char *
get_android_id(JNIEnv *env, jobject mContext, jobject actObj)
{
char *szDevId = NULL; if (!mContext) {
return szDevId;
} jclass resCls = (*env)->FindClass(env, "android/content/Context");
jmethodID getMethod = (*env)->GetMethodID(env, resCls, "getContentResolver", "()Landroid/content/ContentResolver;");
jobject resolver = (*env)->CallObjectMethod(env, actObj, getMethod);
if (resolver == NULL) {
LOGE("Invalid resolver!");
} jclass cls_context = (*env)->FindClass(env, "android/provider/Settings$Secure");
if (cls_context == NULL) {
LOGE("Invalid cls_context!");
} jmethodID getStringMethod = (*env)->GetStaticMethodID(env, cls_context, "getString", "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;");
if (getStringMethod == NULL) {
LOGE("Invalid getStringMethod!");
} jfieldID ANDROID_ID = (*env)->GetStaticFieldID(env, cls_context,
"ANDROID_ID", "Ljava/lang/String;");
jstring str = (jstring)((*env)->GetStaticObjectField(env, cls_context, ANDROID_ID)); jstring jId = (jstring)((*env)->CallStaticObjectMethod(env, cls_context, getStringMethod, resolver, str));
szDevId = (char *)((*env)->GetStringUTFChars(env, jId, 0)); return szDevId;
}

如果是手机,还可以通过TelephoneManager.getDeviceId()方式来获取:

Java代码:

final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();

对应的C代码:

char *
get_telepone_id(JNIEnv *env, jobject mContext, jobject actObj) {
char *szDevId = NULL; if(mContext == 0){
return szDevId;
} jclass cls_context = (*env)->FindClass(env, "android/content/Context"); jmethodID getSystemService = (*env)->GetMethodID(env, cls_context, "getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;"); if(!getSystemService){
return szDevId;
} jfieldID TELEPHONY_SERVICE = (*env)->GetStaticFieldID(env, cls_context,
"TELEPHONY_SERVICE", "Ljava/lang/String;");
if(!TELEPHONY_SERVICE){
return szDevId;
} jstring str = (jstring)(*env)->GetStaticObjectField(env, cls_context, TELEPHONY_SERVICE);
jclass cls_tm = (*env)->FindClass(env, "android/telephony/TelephonyManager");
if(!cls_tm){
return szDevId;
} jobject telephonymanager = (*env)->CallObjectMethod(env, actObj, getSystemService, str);
if(!telephonymanager){
return szDevId;
} jmethodID getDeviceId = (*env)->GetMethodID(env, cls_tm, "getDeviceId", "()Ljava/lang/String;");
if(!getDeviceId){
return szDevId;
}
jstring deviceid = (jstring)(*env)->CallObjectMethod(env, telephonymanager, getDeviceId); szDevId = (char *)(*env)->GetStringUTFChars(env, deviceid, 0); (*env)->DeleteLocalRef(env, cls_tm);
(*env)->DeleteLocalRef(env, cls_context);
(*env)->DeleteLocalRef(env, telephonymanager); return szDevId;
}

二、通过NDK获取证书签名

Java代码:

PackageInfo pis;
try {
pis = mContext.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES); //获取包信息
Signature sigs = pis.signatures[0]; //获取签名
int hash = sigs.hashCode(); //获取签名的哈希码
} catch(Exception e) {
//TODO:log here.
}

对应的C代码:

int
get_apk_signature(JNIEnv *env, jobject obj) { jclass cls = (*env)->FindClass(env, "android/content/ContextWrapper");
//this.getPackageManager();
jmethodID mid = (*env)->GetMethodID(env, cls, "getPackageManager",
"()Landroid/content/pm/PackageManager;");
if (mid == NULL) {
return -1;
} jobject pm = (*env)->CallObjectMethod(env, obj, mid);
if (pm == NULL) {
return -2;
} //this.getPackageName();
mid = (*env)->GetMethodID(env, cls, "getPackageName", "()Ljava/lang/String;");
if (mid == NULL) {
return -3;
} jstring packageName = (jstring)(*env)->CallObjectMethod(env, obj, mid); // packageManager->getPackageInfo(packageName, GET_SIGNATURES);
cls = (*env)->GetObjectClass(env, pm);
mid = (*env)->GetMethodID(env, cls, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
jobject packageInfo = (*env)->CallObjectMethod(env, pm, mid, packageName, 0x40); //GET_SIGNATURES = 64;
cls = (*env)->GetObjectClass(env, packageInfo);
jfieldID fid = (*env)->GetFieldID(env, cls, "signatures", "[Landroid/content/pm/Signature;");
jobjectArray signatures = (jobjectArray)(*env)->GetObjectField(env, packageInfo, fid);
jobject sig = (*env)->GetObjectArrayElement(env, signatures, 0); cls = (*env)->GetObjectClass(env, sig);
mid = (*env)->GetMethodID(env, cls, "hashCode", "()I");
int sig_value = (int)(*env)->CallIntMethod(env, sig, mid);
return sig_value;
}

[转]Android NDK几点回调方式的更多相关文章

  1. Android NDK几点回调方式

    一.NDK中获取android设备ID的方式 Java代码如下(获取设备ANDROID_ID): final String androidId = Secure.getString(context.g ...

  2. Android事件处理的2种方式:监听器与回调

    android组件的事件处理有2种方式: 1.基于监听器的事件处理方式:先定义组件,然后为组件设定监听器. 详见http://blog.csdn.net/jediael_lu/article/deta ...

  3. Android NDK调试出错Unknown Application ABI, Unable to detect application ABI's的解决方式

    今天在调试Android NDK的时候,ADT的控制台报了这个错误: Unknown Application ABI, Unable to detect application ABI's 在网上查了 ...

  4. 初识Android NDK

    本文介绍Windows环境下搭建Android NDK开发环境,并创建一个简单的使用Native代码的Android Application. 一.环境搭建 二.JNI函数绑定 三.例子 一.环境搭建 ...

  5. !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结

    http://hujiaweibujidao.github.io/blog/2013/11/18/android-ndk-and-opencv-development-3/ Android Ndk a ...

  6. Android NDK 和 OpenCV 整合开发总结(3)

    Android NDK 和 OpenCV 整合开发总结(3) http://hujiaweibujidao.github.io/blog/2013/11/18/android-ndk-and-open ...

  7. Android NDK开发之C调用Java及原生代码断点调试(二)

    上一篇中,我们主要学习了Java调用本地方法,并列举了两大特殊实例来例证我们的论据,还没学习的伙伴必须先去阅读下,本次的学习是直接在上一篇的基础上进行了.点击:Android NDK开发之从Java与 ...

  8. 简单的调用OpenCV库的Android NDK开发 工具Android Studio

    前言 本博客写于2017/08/11, 博主非专业搞安卓开发, 只是工作的需要倒腾了下Android NDK相关的开发, 博文中有什么不正确.不严格的地方欢迎指正哈    本文后续也许还会有删改, 就 ...

  9. Android NDK之二:创建NativeActivity

    转:http://blog.csdn.net/xiruanliuwei/article/details/7560914 Android NDK为我们提供了两种方式来实现我们的native activi ...

随机推荐

  1. jquery GET POST

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <head> ...

  2. [译]36 Days of Web Testing(三)

    Day 14: Automate the tedious Why ? 有些时候,web测试还是蛮单调乏味的,在开始测试前,你可能要必须跳转到一个特定的表单页面,或则为了得到一个特定的页面(或配置),你 ...

  3. 一步一步理解 Java 企业级应用的可扩展性

    摘要:本文主要介绍如何理解 Java 应用的扩展方式以及不同类型的扩展技术和具体技巧,介绍一些有关 Java 企业级应用的一般扩展策略. 老实说,"可扩展性"是个全面且详尽的话题, ...

  4. SQL SELECT基本语句结构

    (1)SELECT select_list (2) FROM table_list (3)   WHERE search_conditions     GROUP BY group_by_list   ...

  5. SQL表建立,临时表,表变量示例

    CODE: USE Sales; GO /* CREATE TABLE Orders ( OrderID int IDENTITY(1,1) PRIMARY KEY, OrderGUI uniquei ...

  6. 关于谷歌android 4.3 ble问题

    随着BLE的逐渐壮大,越来越多的厂商开始支持BLE,在未来的一年里应该会慢慢普及,谷歌android4.3已经支持了自己的nexus4和uexus7 2013手机了,也就是说如果厂商采用谷歌已经实现d ...

  7. ☀【Zepto】touch events

    https://github.com/madrobby/zepto jTouchhttps://github.com/liutian1937/jTouch iphone.ipod Touch.ipad ...

  8. 2015第44周五Java集群技术(转)

    从http://blog.csdn.net/cdh1213/article/details/21443239上看到这篇文章,感觉很不错,找好久没找到中文出处,最早看是从http://www.these ...

  9. ASP.net:截取固定长度字符串显示在页面,多余部分显示为省略号

    方法一: public static string GetString(string str, int length) { int i = 0, j = 0; foreach(char chr in ...

  10. cobbler_web安装