主题 : android之应用程序LED(1) 复制链接 | 浏览器收藏 | 打印
菜鸟在路上
级别: 圣骑士
UID: 42749
精华: 6
发帖: 241
金钱: 1530 两
威望: 306 点
贡献值: 6 点
综合积分: 602 分
注册时间: 2011-04-11
最后登录: 2016-07-19
楼主  发表于: 2011-08-03 22:50

 android之应用程序LED(1)

管理提醒: 本帖被 xoom 执行加亮操作(2011-08-05)
参考友善的LED程序,自己也写了个,不是用友善的libfriendlyarm-hardware.so,自己用NDK中的例子hello-jni写了个程序,已经编译成libhello-jni.so,可惜上不了图。

自己摸索了好久总算有点收获,感谢各位网友的帮忙。部门源码贴出来给大家参考! 刚搞完就拿出来分享了,欢迎大家指正。有谁想看libfriendlyarm-hardware.so的源码的可以参考这

个。

JNI部分 用C语言实现

复制代码
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include <jni.h>
  19. #include <fcntl.h> /*包括文件操作,如open() read() close()write()等*/
  20. int fd;
  21. /* This is a trivial JNI example where we use a native method
  22. * to return a new VM String. See the corresponding Java source
  23. * file located at:
  24. *
  25. * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  26. */
  27. jstring
  28. Java_com_helloworld_helloworld_stringFromJNI( JNIEnv* env,
  29. jobject thiz )
  30. {
  31. return (*env)->NewStringUTF(env, "Hello from JNI create by carlin !");
  32. }
  33. jint Java_com_helloworld_helloworld_openled( JNIEnv* env,
  34. jobject thiz )
  35. {
  36. fd = open("/dev/leds0", O_RDONLY);
  37. if (fd < 0)
  38. {
  39. fd = open("/dev/leds", O_RDONLY);
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. jint Java_com_helloworld_helloworld_setLedState( JNIEnv* env,
  45. jobject thiz ,jint ledID,jint ledState)
  46. {
  47. ioctl(fd,ledState, ledID);
  48. return 1;
  49. }
  50. jint Java_com_helloworld_helloworld_closeled( JNIEnv* env,
  51. jobject thiz )
  52. {
  53. close(fd);
  54. return 1;
  55. }


APP部分即JAVA

复制代码
  1. package com.helloworld;
  2. import android.app.Activity;
  3. import android.widget.TextView;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.view.View.OnClickListener;
  8. public class helloworld extends Activity {
  9.  
  10.  private Button btnLED1On;
  11.  private Button btnLED1Off;
  12.     /** Called when the activity is first created. */
  13.     @Override
  14.     public void onCreate(Bundle savedInstanceState)
  15.     {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);
  18.        
  19.         openled();
  20.        
  21.      btnLED1On = (Button)findViewById(R.id.btnLED1On);
  22.      btnLED1Off = (Button)findViewById(R.id.btnLED1Off);
  23.      
  24.      btnLED1On.setOnClickListener(new View.OnClickListener()
  25.      {
  26.       public void onClick(View v) /**当按钮按下*/
  27.       {
  28.        setLedState(0,1);
  29.       }
  30.      }
  31.      );
  32.      
  33.      btnLED1Off.setOnClickListener(new View.OnClickListener()
  34.      {
  35.       public void onClick(View v) /**当按钮按下*/
  36.       {
  37.        setLedState(0,0);
  38.       }
  39.      }
  40.      );
  41.     }
  42.     /* A native method that is implemented by the
  43.      * 'hello-jni' native library, which is packaged
  44.      * with this application.
  45.      */
  46.     public native String  stringFromJNI();
  47.     public native int openled();
  48.     public native int setLedState(int ledID,int ledState);
  49.     public native int closeled();
  50.     /* This is another native method declaration that is *not*
  51.      * implemented by 'hello-jni'. This is simply to show that
  52.      * you can declare as many native methods in your Java code
  53.      * as you want, their implementation is searched in the
  54.      * currently loaded native libraries only the first time
  55.      * you call them.
  56.      *
  57.      * Trying to call this function will result in a
  58.      * java.lang.UnsatisfiedLinkError exception !
  59.      */
  60.     public native String  unimplementedStringFromJNI();
  61.     /* this is used to load the 'hello-jni' library on application
  62.      * startup. The library has already been unpacked into
  63.      * /data/data/com.example.HelloJni/lib/libhello-jni.so at
  64.      * installation time by the package manager.
  65.      */
  66.     static {
  67.         System.loadLibrary("hello-jni");
  68.     }
  69. }
[ 此帖被carlin在2011-08-04 22:47重新编辑 ]
级别: 新手上路
UID: 78899
精华: 0
发帖: 6
金钱: 30 两
威望: 6 点
贡献值: 0 点
综合积分: 12 分
注册时间: 2012-09-26
最后登录: 2017-09-13
1楼  发表于: 2012-09-27 10:24
太好了,谢谢