本文共 6324 字,大约阅读时间需要 21 分钟。
====================================================================================================================================
一、APK编写
1.Android.mk
LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_PACKAGE_NAME := Helloinclude $(BUILD_PACKAGE)2.AndroidManifest.xml
3.src/com/hello/jnitest/Hello.java
package com.hello.jnitest;import com.hello.jnitest.R;//jniimport com.tank.common.Inputproc;//endimport android.app.Activity;import android.os.Bundle;import android.os.RemoteException; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Hello extends Activity implements OnClickListener { private final static String LOG_TAG = "com.hello.jnitest"; private EditText valueText = null; private Button relButton = null; private Button clearrelButton = null; private Button focusButton = null; private Button clearfocusButton = null; private Button writeprocButton = null; private Button cleartestButton = null; //jni private Inputproc test = null; //end @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); valueText = (EditText)findViewById(R.id.edit_value); relButton = (Button)findViewById(R.id.button_rel); clearrelButton = (Button)findViewById(R.id.button_clearrel); focusButton = (Button)findViewById(R.id.button_focus); clearfocusButton = (Button)findViewById(R.id.button_clearfocus); writeprocButton = (Button)findViewById(R.id.button_writeproc); cleartestButton = (Button)findViewById(R.id.button_cleartest); relButton.setOnClickListener(this); clearrelButton.setOnClickListener(this); focusButton.setOnClickListener(this); clearfocusButton.setOnClickListener(this); writeprocButton.setOnClickListener(this); cleartestButton.setOnClickListener(this); //jni test = new Inputproc(); //end } @Override public void onClick(View v) { if(v.equals(relButton)) { //jni setTitle("The Native Add Result is "+String.valueOf(test.changesinglemod(0))); //end } else if(v.equals(clearrelButton)) { //jni setTitle("The Native Add Result is "+String.valueOf(test.changesinglemod(1))); //end } else if(v.equals(focusButton)) { //jni setTitle("The Native Add Result is "+String.valueOf(test.changesinglemod(2))); //end } else if(v.equals(clearfocusButton)) { //jni setTitle("The Native Add Result is "+String.valueOf(test.changesinglemod(3))); //end } else if(v.equals(writeprocButton)) { //jni //String text = valueText.getText().toString(); String text = "0;0;102;1"; setTitle("The Native Add Result is "+String.valueOf(test.input_event_in(text))); String text1 = "0;0;102;0"; setTitle("The Native Add Result is "+String.valueOf(test.input_event_in(text1))); //end } else if(v.equals(cleartestButton)) { String text = ""; valueText.setText(text); } } }4.src/com/tank/common/Inputproc.java
package com.tank.common;public class Inputproc { static { System.loadLibrary("inputproc_jni"); } public native int changesinglemod(int a); public native int input_event_in(String a);}5.res/layout/main.xml
6.res/values/strings.xml
7.res/drawable-mdpi/和res/drawable-hdpi下放图片。 ==================================================================================================================================== 1.Android.mkHello Value Please input a value... relmod relmodclose focusmod focusmodclose Writeproc Cleartest
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_PRELINK_MODULE := falseLOCAL_SRC_FILES := \ com_tank_common_Inputproc.cpp \ inputproc.cppLOCAL_SHARED_LIBRARIES := \ libandroid_runtime \ libutilsLOCAL_MODULE := libinputproc_jniLOCAL_MODULE_TAGS := optionalinclude $(BUILD_SHARED_LIBRARY)2.com_tank_common_Inputproc.cpp
#define LOG_TAG "changemod-JNI"#include3.具体实现inputproc.cpp和inputproc.h实现上述两个函数即可。#include #include #include #include #include #include #include "jni.h"#include "JNIHelp.h"#include "android_runtime/AndroidRuntime.h"#include "inputproc.h"/// static jint com_tank_common_Inputproc_changesinglemod(JNIEnv *env, jobject obj, jint a){ switch(a){ case 0:{ changesinglemod(34); //pipe 0 break; } case 1:{ changesinglemod(35); //pipe 1 break; } case 2:{ changesinglemod(36); //pipe 2 break; } case 3:{ changesinglemod(37); //pipe 3 break; } } return 0;} static jint com_tank_common_Inputproc_input_event_in(JNIEnv *env, jobject obj, jstring a){ char buf[128]; const char* str = env->GetStringUTFChars(a,NULL); LOGD("str is %s\n",str); strcpy(buf,str); LOGD("buf is %s",buf); input_event_in(buf); env->ReleaseStringUTFChars(a,str); return 0;}///static JNINativeMethod gMethods[] = { {"changesinglemod", "(I)I", (void *)com_tank_common_Inputproc_changesinglemod}, {"input_event_in", "(Ljava/lang/String;)I", (void *)com_tank_common_Inputproc_input_event_in},};static int register_Inputproc(JNIEnv *env){ return android::AndroidRuntime::registerNativeMethods(env, "com/tank/common/Inputproc", gMethods, NELEM(gMethods));}jint JNI_OnLoad(JavaVM *vm, void *reserved){ JNIEnv *env = NULL; if (vm->GetEnv((void **)&env, JNI_VERSION_1_4) != JNI_OK) { printf("Error GetEnv\n"); return -1; } assert(env != NULL); if (register_Inputproc(env) < 0) { printf("register_android_test_hello error.\n"); return -1; } return JNI_VERSION_1_4;}
转载地址:http://megai.baihongyu.com/