====================================================================================================================================

一、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

<?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;//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

<?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>

6.res/values/strings.xml

<?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下放图片。
====================================================================================================================================
1.Android.mk

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"#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;}

3.具体实现inputproc.cpp和inputproc.h实现上述两个函数即可。

更多相关文章

  1. androidx.lifecycle.Transformations函数分析
  2. Android(安卓)camera: Metadata\Image从HAL到framework
  3. Android(安卓)Audio代码分析14 - testPlaybackHeadPositionIncre
  4. Android下编写HelloWorld
  5. Android(安卓)ListView滑动回弹——overScrollBy
  6. Qt在Android平台上实现html转PDF的功能
  7. ---RIL层代码分析---RIL_startEventLoop()->eventLoop()->ril_ev
  8. 2020.05.11-Python中调用zip包中的Python脚本
  9. JAVA按钮显示用户名+密码

随机推荐

  1. 想抢先体验Android操作系统的魅力吗?那就
  2. 如何检索Android设备的唯一ID
  3. Android身陷恶意扣费门 部分开发者参与其
  4. Android(安卓)webView不能响应h5打开相册
  5. android数据库事务
  6. Android(安卓)Training - 建立你的第一个
  7. 谈谈我自己对android开发的理解
  8. 【Android】Android聊天机器人实现
  9. 对于android触摸事件模型的一些理解
  10. Android线程模型解析(包括UI的更新)