80,350
社区成员
发帖
与我相关
我的任务
分享
JNIEXPORT jstring JNICALL Hello_Native(JNIEnv *env, jobject obj,jstring string)
{
const char *str = env->GetStringUTFChars(string, 0);
return env->NewStringUTF( "Hello from JNI !");
}
static JNINativeMethod gMethods[] = {
{"JniHello",const_cast<char*>("(Ljava/lang/jsting)Ljava/lang/jsting;"),(void*)Hello_Native},
public native static String JniHello(String text);
private native void _setDataSource(
String path, String[] keys, String[] values)
throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;
{
"_setDataSource",
"(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V",
(void *)android_media_MediaPlayer_setDataSourceAndHeaders
}
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");
}
}
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 jniJNIEXPORT jstring JNICALL
Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)
{
char buf[128];
const jbyte *str;
str = (*env)->GetStringUTFChars(env, prompt, NULL);
if (str == NULL) {
return NULL; /* OutOfMemoryError already thrown */
}
printf("%s", str);
(*env)->ReleaseStringUTFChars(env, prompt, str);
/* We assume here that the user does not type more than
* 127 characters */
scanf("%127s", buf);
return (*env)->NewStringUTF(env, buf);
}
class Prompt {
// native method that prints a prompt and reads a line
private native String getLine(String prompt);
public static void main(String args[]) {
Prompt p = new Prompt();
String input = p.getLine("Type a line: ");
System.out.println("User typed: " + input);
}
static {
System.loadLibrary("Prompt");
}
}