本文主要实现两个功能:
(1)通过Android sdk的API得到应用程序的包名(PackageName),然后传递给c++层函数。
(2)通过c++函数调用Android的java层函数,显示一个对话框,点击按钮退出程序。
1. 首先来简单学习一下JNI的相关知识,我这篇文章中简单实现了怎么在Android Java层调用c++函数。要想使用JNI,必须得包含头文件,android是使用ndk编译c/c++的,这里jni.h文件位于:\android-ndk-r8b\platforms\android-14\arch-arm\usr\include\jni.h,该文件定义了所有和JNI相关的数据类型和接口。下面是相关代码片段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# include <inttypes.h> /* C99 */
typedef
uint8_t jboolean;
/* unsigned 8 bits */
int8_t jbyte;
/* signed 8 bits */
uint16_t jchar;
/* unsigned 16 bits */
int16_t jshort;
/* signed 16 bits */
int32_t jint;
/* signed 32 bits */
int64_t jlong;
/* signed 64 bits */
double
jdouble;
/* 64-bit IEEE 754 */
#else
unsigned
char
jboolean;
/* unsigned 8 bits */
signed
char
jbyte;
/* signed 8 bits */
short
jchar;
/* unsigned 16 bits */
short
jshort;
/* signed 16 bits */
int
jint;
/* signed 32 bits */
long
long
jlong;
/* signed 64 bits */
/* 32-bit IEEE 754 */
/* 64-bit IEEE 754 */
#endif
/* "cardinal indices and sizes" */
jint jsize;
#ifdef __cplusplus
/*
* Reference types,in C++
*/
@H_404_250@class
_jobject {};
_jstring :
_jobject {};
_jarray :
_jobject {};
_jobjectArray :
_jarray {};
_jbooleanArray :
_jarray {};
_jbyteArray :
_jarray {};
_jcharArray :
_jarray {};
_jshortArray :
_jarray {};
_jintArray :
_jarray {};
_jlongArray :
_jarray {};
_jfloatArray :
_jarray {};
_jdoubleArray :
_jarray {};
_jthrowable :
_jobject {};
_jobject* jobject;
_jclass* jclass;
_jstring* jstring;
_jarray* jarray;
_jobjectArray* jobjectArray;
_jbooleanArray* jbooleanArray;
_jbyteArray* jbyteArray;
_jcharArray* jcharArray;
_jshortArray* jshortArray;
_jintArray* jintArray;
_jlongArray* jlongArray;
_jfloatArray* jfloatArray;
_jdoubleArray* jdoubleArray;
_jthrowable* jthrowable;
_jobject* jweak;
#else /* not __cplusplus */
/*
jobject jclass;
jobject jstring;
jobject jarray;
jarray jobjectArray;
jarray jbooleanArray;
jarray jbyteArray;
jarray jcharArray;
jarray jshortArray;
jarray jintArray;
jarray jlongArray;
jarray jfloatArray;
jarray jdoubleArray;
jobject jthrowable;
jobject jweak;
#endif /* not __cplusplus */
|
我们经常用到的是JNIEnv*,它是一个c结构体,封装了许多常用的函数,如:
@H_404_38@