博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Android系统学习》之JAVA与C混合编程——JNI
阅读量:4180 次
发布时间:2019-05-26

本文共 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

Hello
Value
Please input a value...
relmod
relmodclose
focusmod
focusmodclose
Writeproc
Cleartest
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 
#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;}
3.具体实现inputproc.cpp和inputproc.h实现上述两个函数即可。

转载地址:http://megai.baihongyu.com/

你可能感兴趣的文章
Dubbo特性结果缓存、并发与连接控制
查看>>
dubbo服务分组、限流措施以及服务熔断降级
查看>>
Dubbo特性之本地存根、本地伪装和隐式参数传递
查看>>
Oracle恢复某个时间之前的数据
查看>>
Spring Cloud Alibaba 教程 | 安装 Nacos(一)
查看>>
Mysql生成连续的日期统计,没有结果的补充为0
查看>>
Spring Cloud Alibaba 教程 | 将服务注册到 Nacos(二)
查看>>
Spring Cloud Alibaba 教程 | Nacos 配置中心(三)
查看>>
Spring Cloud Alibaba 教程 | Nacos 集成 Feign(四)
查看>>
Git切换项目当前登录账号
查看>>
Spring security 基本原理(一)
查看>>
Spring Security自定义用户认证逻辑
查看>>
Spring Security 自定义表单登录流程
查看>>
uni app页面跳转后,刷新页面参数丢失问题
查看>>
Spring Security 图形验证码校验
查看>>
Spring Security 自定义短信验证码登录
查看>>
Vue中安装高得地图AmapVue 组件,根据坐标获取位置名称
查看>>
JSP的书写规范及工作原理
查看>>
JSP应用中的EL表达式
查看>>
SpringBoot的概念及入门
查看>>