Today, we have a requirement that we need to call the methods in jni twice in android. This has never been implemented before, so we need to write a demo to practice it.
: Http://www.bkjia.com/uploadfile/2011/1202/20111202032600450.zip
:
Code:
Package com. boao;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class JniIntActivity extends Activity {
/** Called when the activity is first created .*/
TextView textView;
TextView textView2;
Public static final String libName = "JniInt ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView = (TextView) findViewById (R. id. text );
TextView2 = (TextView) findViewById (R. id. text2 );
// String string = String. valueOf (getSum ());
TextView. setText ("Total number:" + getSum ());
TextView2.setText ("the number is:" + getDate () + "");
}
Static
{
System. loadLibrary (libName );
}
/**
* Obtain the total number.
* @ Return
*/
Public native int getSum ();
/**
* Get Data
* @ Return
*/
Public native int getDate ();
}
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
<TextView
Android: id = "@ + id/text2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
</LinearLayout>
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_C_INCLUDES: = $ (LOCAL_PATH)/include
LOCAL_LDLIBS + =-L $ (SYSROOT)/usr/lib-llog
LOCAL_MODULE: = JniInt
LOCAL_SRC_FILES: = \
JavaCalu. c \
Include $ (BUILD_SHARED_LIBRARY)
# Include <string. h>
# Include <android/log. h>
# Include <jni. h>
/**
* Call methods in jni
*/
JNIEXPORT jint JNICALL Java_com_boao_JniIntActivity_getSum (JNIEnv * env, jobject thiz ){
Jint I = 0;
I ++;
Return getsum (env, I );
}
Jint getsum (JNIEnv * env, int date ){
Jint I = 1;
For (I = 0; I <10; I ++ ){
Date + = I;
}
Return date;
}
JNIEXPORT jint JNICALL Java_com_boao_JniIntActivity_getDate (JNIEnv * env, jobject thiz)
{
Jint I = 10;
Return I;
}
From the column of haojunming