管理提醒: 本帖被 xoom 执行加亮操作(2011-08-05)
参考友善的LED程序,自己也写了个,不是用友善的libfriendlyarm-hardware.so,自己用NDK中的例子hello-jni写了个程序,已经编译成libhello-jni.so,可惜上不了图。
自己摸索了好久总算有点收获,感谢各位网友的帮忙。部门源码贴出来给大家参考! 刚搞完就拿出来分享了,欢迎大家指正。有谁想看libfriendlyarm-hardware.so的源码的可以参考这
个。
JNI部分 用C语言实现
复制代码-
- /*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- #include <string.h>
- #include <jni.h>
- #include <fcntl.h> /*包括文件操作,如open() read() close()write()等*/
- int fd;
- /* This is a trivial JNI example where we use a native method
- * to return a new VM String. See the corresponding Java source
- * file located at:
- *
- * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
- */
- jstring
- Java_com_helloworld_helloworld_stringFromJNI( JNIEnv* env,
- jobject thiz )
- {
- return (*env)->NewStringUTF(env, "Hello from JNI create by carlin !");
- }
- jint Java_com_helloworld_helloworld_openled( JNIEnv* env,
- jobject thiz )
- {
- fd = open("/dev/leds0", O_RDONLY);
- if (fd < 0)
- {
- fd = open("/dev/leds", O_RDONLY);
- return 1;
- }
- return 0;
- }
- jint Java_com_helloworld_helloworld_setLedState( JNIEnv* env,
- jobject thiz ,jint ledID,jint ledState)
- {
- ioctl(fd,ledState, ledID);
- return 1;
- }
- jint Java_com_helloworld_helloworld_closeled( JNIEnv* env,
- jobject thiz )
- {
- close(fd);
- return 1;
- }
|
APP部分即JAVA
复制代码- package com.helloworld;
- import android.app.Activity;
- import android.widget.TextView;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.view.View.OnClickListener;
- public class helloworld extends Activity {
-
- private Button btnLED1On;
- private Button btnLED1Off;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- openled();
-
- btnLED1On = (Button)findViewById(R.id.btnLED1On);
- btnLED1Off = (Button)findViewById(R.id.btnLED1Off);
-
- btnLED1On.setOnClickListener(new View.OnClickListener()
- {
- public void onClick(View v) /**当按钮按下*/
- {
- setLedState(0,1);
- }
- }
- );
-
- btnLED1Off.setOnClickListener(new View.OnClickListener()
- {
- public void onClick(View v) /**当按钮按下*/
- {
- setLedState(0,0);
- }
- }
- );
- }
- /* A native method that is implemented by the
- * 'hello-jni' native library, which is packaged
- * with this application.
- */
- public native String stringFromJNI();
- public native int openled();
- public native int setLedState(int ledID,int ledState);
- public native int closeled();
- /* This is another native method declaration that is *not*
- * implemented by 'hello-jni'. This is simply to show that
- * you can declare as many native methods in your Java code
- * as you want, their implementation is searched in the
- * currently loaded native libraries only the first time
- * you call them.
- *
- * Trying to call this function will result in a
- * java.lang.UnsatisfiedLinkError exception !
- */
- public native String unimplementedStringFromJNI();
- /* this is used to load the 'hello-jni' library on application
- * startup. The library has already been unpacked into
- * /data/data/com.example.HelloJni/lib/libhello-jni.so at
- * installation time by the package manager.
- */
- static {
- System.loadLibrary("hello-jni");
- }
- }
|
[ 此帖被carlin在2011-08-04 22:47重新编辑 ]