package com.yang.jniaccesshardware;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.Toast;import com.yang.hardlibrary.HardControl;public class MainActivity extends AppCompatActivity {    private boolean led_on = false;    private Button button;    private CheckBox led1;    private CheckBox led2;    private CheckBox led3;    private CheckBox led4;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button=(Button)findViewById(R.id.BUTTON);        HardControl.ledOpen();        led1=(CheckBox)findViewById(R.id.led1);        led2=(CheckBox)findViewById(R.id.led2);        led3=(CheckBox)findViewById(R.id.led3);        led4=(CheckBox)findViewById(R.id.led4);        button.setOnClickListener(new MyButtonListener());    }    class MyButtonListener implements View.OnClickListener {        @Override        public void onClick(View v) {            HardControl hardControl = new HardControl();//依赖硬件            led_on = !led_on;            if (led_on) {                button.setText("ALL OFF");                led1.setChecked(true);                led2.setChecked(true);                led3.setChecked(true);                led4.setChecked(true);                for (int i = 0; i < 4; i++)                    HardControl.ledCtrl(i, 1);            }            else {                button.setText("ALL ON");                led1.setChecked(false);                led2.setChecked(false);                led3.setChecked(false);                led4.setChecked(false);                for (int i = 0; i < 4; i++)                    HardControl.ledCtrl(i, 0);            }        }    }    public void onCheckboxClicked(View view) {        // Is the view now checked?        boolean checked = ((CheckBox) view).isChecked();        // Check which checkbox was clicked        switch(view.getId()) {            case R.id.led1:                if (checked)                {                    Toast.makeText(getApplicationContext(),"led1_on",Toast.LENGTH_LONG).show();                    HardControl.ledCtrl(1, 1);                }                else                {                    Toast.makeText(getApplicationContext(),"led1_off",Toast.LENGTH_LONG).show();                    HardControl.ledCtrl(1, 0);                }                break;            case R.id.led2:                if (checked)                {                    Toast.makeText(getApplicationContext(),"led1_on",Toast.LENGTH_LONG).show();                    HardControl.ledCtrl(2, 1);                }                else                {                    Toast.makeText(getApplicationContext(),"led1_off",Toast.LENGTH_LONG).show();                    HardControl.ledCtrl(2, 0);                }                break;            case R.id.led3:                if (checked)                {                    Toast.makeText(getApplicationContext(),"led1_on",Toast.LENGTH_LONG).show();                    HardControl.ledCtrl(3, 1);                }                else                {                    Toast.makeText(getApplicationContext(),"led1_off",Toast.LENGTH_LONG).show();                    HardControl.ledCtrl(3, 0);                }                break;            case R.id.led4:                if (checked)                {                    Toast.makeText(getApplicationContext(),"led1_on",Toast.LENGTH_LONG).show();                    HardControl.ledCtrl(4, 1);                }                else                {                    Toast.makeText(getApplicationContext(),"led1_off",Toast.LENGTH_LONG).show();                    HardControl.ledCtrl(4, 0);                }                break;        }    }}

 

 

package com.yang.hardlibrary;public class HardControl {    public static native int ledCtrl(int which, int status);    public static native int ledOpen();    public static native void ledClose();//申明三个本地方法。    static {        try {            System.loadLibrary("hardcontrol");        } catch (Exception e) {            e.printStackTrace();        }    }}

 

#include   /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */#include #include #include #include #include #include #include   /* liblog *///__android_log_print(ANDROID_LOG_DEBUG, "JNIDemo", "native add ..."); #if 0typedef struct {    char *name;          /* Java里调用的函数名 */    char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */    void *fnPtr;          /* C语言实现的本地函数 */} JNINativeMethod;#endifstatic jint fd;jint ledOpen(JNIEnv *env, jobject cls){    fd = open("/dev/leds", O_RDWR);    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledOpen : %d", fd);    if (fd >= 0)        return 0;    else        return -1;}void ledClose(JNIEnv *env, jobject cls){    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledClose ...");    close(fd);}jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status){    int ret = ioctl(fd, status, which);    __android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledCtrl : %d, %d, %d", which, status, ret);    return ret;}static const JNINativeMethod methods[] = {    {"ledOpen", "()I", (void *)ledOpen},    {"ledClose", "()V", (void *)ledClose},    {"ledCtrl", "(II)I", (void *)ledCtrl},};/* System.loadLibrary */JNIEXPORT jint JNICALLJNI_OnLoad(JavaVM *jvm, void *reserved){    JNIEnv *env;    jclass cls;    if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {        return JNI_ERR; /* JNI version not supported */    }    cls = (*env)->FindClass(env, "com/yang/hardlibrary/HardControl");    if (cls == NULL) {        return JNI_ERR;    }    /* 2. map java hello <-->c c_hello */    if ((*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0])) < 0)        return JNI_ERR;    return JNI_VERSION_1_4;}

 

转载于:https://www.cnblogs.com/maogefff/p/7738084.html

更多相关文章

  1. android 7.0 user版本调试方法
  2. Android(安卓)SQLite数据库增删改查
  3. android支付宝 KeyFactory PrivateKey
  4. TypedefViewTest
  5. 选择型控件
  6. android个推消息推送,asp.net调用接口
  7. Android(安卓)API Level 与 Platform Version之间的关系
  8. DrawLayout几个注意点
  9. AES加解密源码(直接可调用)

随机推荐

  1. 在页面添加一个透明Layout
  2. android 通过命令行启动Apk
  3. android Activity跳转到指定的Fragment
  4. android源码下载与编译过程记录
  5. Android(安卓)studio 使用android.mk来配
  6. 联想笔记本运行Android(安卓)Studio时无
  7. Android中的Activity Action大全
  8. android onContextItemSelected和onMenuI
  9. 解决Android中解析xml时遇到的MalformedU
  10. Android(安卓)4.1 APIs