jni怎么传递一个字符串数组

以NDK自带hellojni为例

/* HelloJni.java中的Java code */

public class HelloJni extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/*
		 * Create a TextView and set its content. the text is retrieved by
		 * calling a native function.
		 */
		TextView tv = new TextView(this);
		tv.setText(stringFromJNI("jialiry"));
		setContentView(tv);
	}

	/*
	 * A native method that is implemented by the 'hello-jni' native library,
	 * which is packaged with this application.
	 */
	public native String stringFromJNI(String str);

	/*
	 * This is another native method declaration that is *not* implemented by
	 * 'hello-jni'. This is simply to show that you can declare as many native
	 * methods in your Java code as you want, their implementation is searched
	 * in the currently loaded native libraries only the first time you call
	 * them.
	 * 
	 * Trying to call this function will result in a
	 * java.lang.UnsatisfiedLinkError exception !
	 */
	public native String unimplementedStringFromJNI();

	/*
	 * this is used to load the 'hello-jni' library on application startup. The
	 * library has already been unpacked into
	 * /data/data/com.example.hellojni/lib/libhello-jni.so at installation time
	 * by the package manager.
	 */
	static {
		System.loadLibrary("hello-jni");
	}
}

/* hello-jni.c中的C/C++ code */

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
		jobject thiz, jstring jstr)
{
	char buf[128];
	const char* string=(char*)(*env)->GetStringUTFChars(env,jstr,NULL);
	strcpy(buf, string);
	strcat(buf, "同志,hello from jni");
	(*env)->ReleaseStringUTFChars(env,jstr,(jbyte*)string);
	return (*env)->NewStringUTF(env, buf);
}

/*结果:

jialiry同志,hello from jni

*/



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值