1. Andoird JNINativeMethod 定义如下:
typedef struct {
const char* fname;
const char* sg;
void* fn;
} JNINativeMethod;
fname -- function name in java
sg -- character string used in descripting parameters and return value of function
fn -- function pointer;
2. parameter and return value
as far as sg in JNINativeMethod, followings are some examples
"()V" -- void func();
"(II)V" -- void func(int, int);
"(Ljava/lang/string; Ljava/lang/string)V" -- void func(string, string)
"(Ljava/lang/object; Ljava/lang/object; Ljava/lang/object)V" -- void func(class, class, class);
3. Map Relatoin between java and C/C++
Character Java Type C/C++ Type
V void void
Z boolean jboolean
I int jint
J long jlong
D double jdouble
F float jfloat
B byte jbyte
C char jchar
S short jshort
[I int[] jintArray
[F float[] jfloatArray
[B byte[] jbyteArray
[C char[] jcharArray
[S short[] jshortArray
[D double[] jdoubleArray
[J long[] jlongArray
[Z boolean[] jbooleanArray
Ljava/lang/String String jstring
Ljava/net/Socket Socket jobject
4. example
method descripition Java Method
// Notes: ";" is a must
"()Ljava/lang/String;" ------- string func();
"(ILjava/lang/String;)J" ------- long func(int, string);
5 sample
5.1. java end
class ResourceService {
public SyncMode sync_mode;
public FileSystemType filesystem_type;
public String service_ip;
public int service_port;
public String login_username;
public String login_passwd;
public String resource_path;
public String resource_name;
public String resource_ext;
public ResourceService() {
}
public SyncMode getSyncMode() {
return sync_mode;
}
public void setSyncMode(SyncMode mode) {
sync_mode = mode;
}
public FileSystemType getFileSystemType() {
return filesystem_type;
}
public void setFileSystemType(FileSystemType type) {
filesystem_type = type;
}
public String getServiceIp() {
return service_ip;
}
public void setServiceIp(String ip) {
service_ip = ip;
}
public int getServicePort() {
return service_port;
}
public void setServicePort(int port) {
service_port = port;
}
public String getLoginUserName() {
return login_username;
}
public void setLoginUserName(String name) {
login_username = name;
}
public String getLoginPasswd() {
return login_passwd;
}
public void setLoginPasswd(String passwd) {
login_passwd = passwd;
}
public String getResourcePath() {
return resource_path;
}
public void setResourcePath(String path) {
resource_path = path;
}
public String getResourceName() {
return resource_name;
}
public void setResourceName(String name) {
resource_name = name;
}
public String getResourceExt() {
return resource_ext;
}
public void setResourceExt(String ext) {
resource_ext = ext;
}
}
ResourceService rss = new ResourceService();
rss.setSyncMode(1);
.........................................
rss.setResourcePath("/");
LiveStream = LiveStream(rss);
class LiveStream {
ResourceService rss;
public LiveStream(ResourceService rss) {
this.rss = rss;
downStream();
}
public void downStream() {
(new Thread() {
@Override
public void run() {
native_down_stream(rss);
}
}).start();
}
static {
System.loadLibrary("liveclient");
}
}
5.2. c/c++ end
5.2.1 register
static JNINativeMethod methods[] = {
{"native_livestream", "(Ljava/lang/object)V", (void*) native_livestream},
or
{"native_livestream", "(Lcom/live/ResourceService)V", (void*) native_livestream},
{"native_live_getchannel", "()Ljava/lang/String;", (void*) native_live_getchannel},
};
5.2.2 declaration
#include <jni.h>
#include <android/log.h>
#define LOG_TAG "LiveClient"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
void native_livestream(JNIEnv *env UNUSED, jclass clazz UNUSED, jobject obj);
5.2.3 define and implementation
void native_livestream(JNIEnv *env UNUSED, jclass clazz UNUSED, jobject obj)
{
// ResourceService *rss = (ResourceService *)obj;
ResourceService *rss = NULL;
Sco_NET_RC rc;
int fd = -1;
jclass cls;
jfieldID jmode_fid, jtype_fid, juser_fid, jpwd_fid;
jfieldID jip_fid, jport_fid, jpath_fid, jname_fid, jext_fid;
jint jmode, jtype, jport;
jstring juser, jpwd, jip, jpath, jname, jext;
int mode, type, port;
char *user, *pwd, *ip, *path, *name, *ext;
if(obj == NULL) {
LOGI("Null Pointer in parameters in [%s].\n", __FUNCTION__);
return;
}
rss = new ResourceService();
if(!rss) {
LOGI("Fail to alloc memory.\n");
return;
}
// cls = env->FindClass("com/uw/ResourceService");
cls = env->GetObjectClass(obj);
if(cls == NULL) {
LOGI("Fail to find class com.uw.ResourceService.\n");
return;
}
jmode_fid = env->GetFieldID(cls, "sync_mode", "I");
jtype_fid = env->GetFieldID(cls, "filesystem_type", "I");
juser_fid = env->GetFieldID(cls, "login_username", "Ljava/lang/String");
jpwd_fid = env->GetFieldID(cls, "login_passwd", "Ljava/lang/String");
jip_fid = env->GetFieldID(cls, "service_ip", "Ljava/lang/String");
jport_fid = env->GetFieldID(cls, "service_port", "I");
jpath_fid = env->GetFieldID(cls, "resource_path", "Ljava/lang/String");
jname_fid = env->GetFieldID(cls, "resource_name", "Ljava/lang/String");
jext_fid = env->GetFieldID(cls, "resource_ext", "Ljava/lang/String");
jmode = env->GetIntField(obj, jmode_fid);
jtype = env->GetIntField(obj, jtype_fid);
jport = env->GetIntField(obj, jport_fid);
jip = (jstring )env->GetObjectField(obj, jip_fid);
juser = (jstring) env->GetObjectField(obj, juser_fid);
jpwd = (jstring)env->GetObjectField(obj, jpwd_fid);
jpath = (jstring)env->GetObjectField(obj, jpath_fid);
jname = (jstring)env->GetObjectField(obj, jname_fid);
jext = (jstring)env->GetObjectField(obj, jext_fid);
rss->sync_mode = jmode;
rss->filesystem_type = jtype;
rss->service_port = jport;
rss->service_ip = (uint8_t *)env->GetStringUTFChars(jip, NULL);
rss->login_username = (uint8_t *)env->GetStringUTFChars(juser, NULL);
rss->login_passwd = (uint8_t *)env->GetStringUTFChars(jpwd, NULL);
rss->resource_path = (uint8_t *)env->GetStringUTFChars(jpath, NULL);
rss->resource_name = (uint8_t *)env->GetStringUTFChars(jname, NULL);
rss->resource_ext = (uint8_t *)env->GetStringUTFChars(jext, NULL);
................................................................................................................................
}
java end