TQ210搭载Android4.0.3系统构建之LED从驱动到HAL到JNI到应用程序(应用程序篇)

开发板:TQ210

OS:Android 4.0.3

以下所有内容都是在TQ210开发板上实现,并且很多内容也是天嵌公司提供,我将一些内容进行了删减、替换,然后加入了一些自己的理解,同时也是记录自己学习的旅程。

LedUnderActivity.java

package com.unders.led;

import com.unders.led.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class LedUnderActivity extends Activity implements OnCheckedChangeListener{
    /** Called when the activity is first created. */
	Switch led1,led2,all;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        led1=(Switch) findViewById(R.id.led1);  //控制LED的开关
        led2=(Switch) findViewById(R.id.led2);
        all=(Switch) findViewById(R.id.led3);    //控制两个LED的开关
        led1.setOnCheckedChangeListener(this);   //开关的事件监听
        led2.setOnCheckedChangeListener(this);
        all.setOnCheckedChangeListener(this);
        if(!led_init())  //led初始化 加载库 初始化led 打开led
        {
        	new AlertDialog.Builder(LedUnderActivity.this).setTitle("error").setMessage("init led fail
").setPositiveButton("确定", null).show();
        }
    }
    
    //加载libledunders.so
    static{
    	System.loadLibrary("ledunders");  
    }
    
    //本地函数
    public static native boolean led_init();
    public static native boolean led_close();
    public static native boolean led_setOn(int number);
    public static native boolean led_setOff(int number);
    
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch (buttonView.getId()) {
		case R.id.led1:  //操纵LED1
			//Toast.makeText(LedActivity.this, "1", 1).show();
			//if (led1.isChecked()) {
				controlLed(1,led1.isChecked());  
				Log.d("msg", "操作LED1,"+led1.isChecked());
			//}
			break;
		case R.id.led2:  //操纵LED2
			//Toast.makeText(LedActivity.this, "2", 1).show();
			controlLed(2,led2.isChecked());
			Log.d("msg", "操作LED2,"+led2.isChecked());
					break;
		case R.id.led3:  //同时操纵两个LED
			//Toast.makeText(LedActivity.this, "12", 1).show();
			controlLed(1,all.isChecked());
			controlLed(2,all.isChecked());
			Log.d("msg", "操作LED1和LED2,"+all.isChecked());
			break;
		}
	}
	
	private void controlLed(int number,boolean on)  //控制LED函数
	{
		if (on) {
			led_setOn(number);
		}else {
			led_setOff(number);
		}
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {  //退出
		// TODO Auto-generated method stub
		menu.add(0, 1, 0, "退出");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case 1:
			this.finish();
			break;

		default:
			break;
		}
		return true;
	}
    
}


 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/led1" />

    <Switch
        android:id="@+id/led1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
         />
</LinearLayout>
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/led2" />

    <Switch
        android:id="@+id/led2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
         />
</LinearLayout>
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/All" />

    <Switch
        android:id="@+id/led3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
         />
</LinearLayout>
</LinearLayout>


 

效果图:

   对于上层的应用开发 比较简单 通过JNI调用本地方法 操纵LED的亮或者灭

原文地址:https://www.cnblogs.com/liangxinzhi/p/4275630.html