====================================================================================================================================
一、APK编写
1.Android.mk
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-subdir-java-files) LOCAL_PACKAGE_NAME := Hello include $(BUILD_PACKAGE)2.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hello.jnitest" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="Bug Report Sender" > <activity android:name=".Hello" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>3.src/com/hello/jnitest/Hello.java
package com.hello.jnitest; import com.hello.jnitest.R; //jni import com.tank.common.Inputproc; //end import 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); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/value"> </TextView> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/edit_value" android:hint="@string/hint"> </EditText> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/button_rel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rel"> </Button> <Button android:id="@+id/button_clearrel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clearrel"> </Button> <Button android:id="@+id/button_focus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/focus"> </Button> <Button android:id="@+id/button_clearfocus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clearfocus"> </Button> <Button android:id="@+id/button_writeproc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/writeproc"> </Button> <Button android:id="@+id/button_cleartest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cleartest"> </Button> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Hello</string> <string name="value">Value</string> <string name="hint">Please input a value...</string> <string name="rel">relmod</string> <string name="clearrel">relmodclose</string> <string name="focus">focusmod</string> <string name="clearfocus">focusmodclose</string> <string name="writeproc">Writeproc</string> <string name="cleartest">Cleartest</string> </resources>7.res/drawable-mdpi/和res/drawable-hdpi下放图片。
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_PRELINK_MODULE := false LOCAL_SRC_FILES := \ com_tank_common_Inputproc.cpp \ inputproc.cpp LOCAL_SHARED_LIBRARIES := \ libandroid_runtime \ libutils LOCAL_MODULE := libinputproc_jni LOCAL_MODULE_TAGS := optional include $(BUILD_SHARED_LIBRARY)2.com_tank_common_Inputproc.cpp
#define LOG_TAG "changemod-JNI" #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <assert.h> #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; }