android之应用程序LED(1)

管理提醒: 本帖被 xoom 执行加亮操作(2011-08-05)

参考友善的LED程序,自己也写了个,不是用友善的libfriendlyarm-hardware.so,自己用NDK中的例子hello-jni写了个程序,已经编译成libhello-jni.so,可惜上不了图。 

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

个。

JNI部分 用C语言实现

复制代码


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


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.                 }