android中两次调用jni的方法
今天有一个需求,需要在android中两次调用jni里面的方法,这个以前没有实现过 ,所以写一个demo练习一下。
下载地址: /2011/1202/20111202032600450.zip
效果图:
上代码:
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("总数是:"+getSum());
textView2.setText("数字是:"+getDate()+"");
}
static
{
System.loadLibrary(libName);
}
/**
* 得到总数
* @return
*/
public native int getSum();
/**
* 得到数据
* @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>
/**
* 在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;
}
摘自 haojunming的专栏
补充:移动开发 , Android ,