Home > Database > Mysql Tutorial > 学习android JNI的那些事儿--------5. Field & Method --&g

学习android JNI的那些事儿--------5. Field & Method --&g

WBOY
Release: 2016-06-07 15:31:13
Original
951 people have browsed it

在java编程语言中有非静态成员函数和静态成员函数,JNI允许我们访问到java中的成员函数,然后再jni中调用,这里我就来举例说明在jni中是如何做到的。 我们先在java中定义2个成员函数,一个非静态的,一个是静态的,分别会把title的textView设置成不同的: pu

在java编程语言中有非静态成员函数和静态成员函数,JNI允许我们访问到java中的成员函数,然后再jni中调用,这里我就来举例说明在jni中是如何做到的。

我们先在java中定义2个成员函数,一个非静态的,一个是静态的,分别会把title的textView设置成不同的值:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContext = this;
        bt1 = (Button)findViewById(R.id.button1);
        bt2 = (Button)findViewById(R.id.button2);
        tv = (TextView)findViewById(R.id.tv);
        tv.setText(""+si);
        bt1.setOnClickListener(new MyButtonListener());
        bt2.setOnClickListener(new MyButtonListener());
    }
    class MyButtonListener implements OnClickListener{

		public void onClick(View v) {
			if(v.getId() == R.id.button1 ){
				accessInstanceMethod("Instance Method Call");
			}
			if(v.getId() == R.id.button2 ){
				accessInstanceMethod("Static Method Call");
			}
		}
    }
    private void InstanceMethod(String str)
    {
    	tv.setText(str);
    }
    private void static StaticMethod(String str)
    {
    	tv.setText(str);
    }
    public native void accessInstanceMethod(String str);
    public native void accessStaticMethod(String str);
Copy after login

我们再来看下jni中是如何处理的:

void 
Java_com_android_jni_MyJNI_accessInstanceMethod(JNIEnv *env,
		jobject obj, jstring jstr)
{
	const jbyte *str;
	str = (*env)->GetStringUTFChars(env,jstr,NULL);
	if(str == NULL)
		return ;
	__android_log_print(ANDROID_LOG_INFO,"-JNI-","%s",str);
	(*env)->ReleaseStringUTFChars(env,jstr,str);

	jclass cls = (*env)->GetObjectClass(env,obj);
	jmethodID mid = 
		(*env)->GetMethodID(env,cls,"InstanceMethod",
				"(Ljava/lang/String;)V");
	if(mid == NULL)
		return ;
	(*env)->CallVoidMethod(env,obj,mid,jstr);
}

void
Java_com_android_jni_MyJNI_accessStaticMethod(JNIEnv *env,
		jobject obj, jstring jstr)
{
	const jbyte *str;
	str = (*env)->GetStringUTFChars(env,jstr,NULL);
	if(str == NULL)
		return;
	__android_log_print(ANDROID_LOG_INFO,"-JNI-","%s",str);
	(*env)->ReleaseStringUTFChars(env,jstr,str);

	jclass cls = (*env)->GetObjectClass(env,obj);
	jmethodID mid = 
		(*env)->GetStaticMethodID(env,cls,"StaticMethod",
				"(Ljava/lang/String;)V");
	if(mid == NULL)
		return ;
	(*env)->CallStaticVoidMethod(env,obj,mid,jstr);
}
Copy after login

这边都比较简单,先是找到class,然后得到method ID,之后是根据method ID调用JAVA中的function

运行模拟器,点击不同的按钮观察title的textView:


-----------------------------------------------------------------------------------------------------

调用java的method就介绍到这,关于别的返回值的函数大家可以自己去尝试,用法都是差不多的。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template