android平台led开发之应用层(四)

 
1,为了方便,使用Eclipse编写一个简单的控制灯的应用,添加四个button,分配控制灯1亮,灯1灭,灯2亮,灯2灭,Activity代码如下

点击(此处)折叠或打开

  1. package com.android.swtled;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.ISwtledService;
  5. import android.os.ServiceManager;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.os.RemoteException; 

  11. public class swtled extends Activity {
  12.     
  13.     private static final String LOG_TAG = "com.android.swtled";
  14.     private ISwtledService swtledService = null;
  15.     
  16.     private Button setOnLight1Button = null;
  17.     private Button setOffLight1Button = null;
  18.     private Button setOnLight2Button = null;
  19.     private Button setOffLight2Button = null;
  20.     
  21.     /** Called when the activity is first created. */
  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.main);

  26.         swtledService = ISwtledService.Stub.asInterface(ServiceManager.getService("swtled"));

  27.         setOnLight1Button = (Button)findViewById(R.id.button_seton_light1);
  28.         setOffLight1Button = (Button)findViewById(R.id.button_setoff_light1);
  29.         setOnLight2Button = (Button)findViewById(R.id.button_seton_light2);
  30.         setOffLight2Button = (Button)findViewById(R.id.button_setoff_light2);

  31.         setOnLight1Button.setOnClickListener(new OnClickListener()
  32.         {

  33.             @Override
  34.             public void onClick(View v) {
  35.                 // TODO Auto-generated method stub

  36.                 try{
  37.                     
  38.                     swtledService.setOn(1);
  39.                 }catch(RemoteException e)
  40.                 {
  41.                     Log.e(LOG_TAG, "Exception while setOn Light."); 

  42.                 }
  43.             }
  44.             
  45.         });
  46.         
  47.         setOffLight1Button.setOnClickListener(new OnClickListener()
  48.         {

  49.             @Override
  50.             public void onClick(View v) {
  51.                 // TODO Auto-generated method stub

  52.                 try{
  53.                     
  54.                     swtledService.setOff(1);
  55.                 }catch(RemoteException e)
  56.                 {
  57.                     Log.e(LOG_TAG, "Exception while setOn Light."); 

  58.                 }
  59.             }
  60.             
  61.         });
  62.         
  63.         setOnLight2Button.setOnClickListener(new OnClickListener()
  64.         {

  65.             @Override
  66.             public void onClick(View v) {
  67.                 // TODO Auto-generated method stub

  68.                 try{
  69.                     
  70.                     swtledService.setOn(2);
  71.                 }catch(RemoteException e)
  72.                 {
  73.                     Log.e(LOG_TAG, "Exception while setOn Light."); 

  74.                 }
  75.             }
  76.             
  77.         });
  78.         
  79.         setOffLight2Button.setOnClickListener(new OnClickListener()
  80.         {

  81.             @Override
  82.             public void onClick(View v) {
  83.                 // TODO Auto-generated method stub

  84.                 try{
  85.                     
  86.                     swtledService.setOff(2);
  87.                 }catch(RemoteException e)
  88.                 {
  89.                     Log.e(LOG_TAG, "Exception while setOn Light."); 

  90.                 }
  91.             }
  92.         });
  93.     }
  94. }
2, AndroidManifest.xml脚本

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.android.swtled"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">


  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".swtled"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>    
  14.     </application>
  15. </manifest>

3, 字符串文件res/values/strings.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <string name="hello">Hello World, swtled!>
  4.     <string name="app_name">swtled</string>
  5.     <string name="seton_light1"></string>
  6.     <string name="setoff_light1"></string>
  7.     <string name="seton_light2"></string>
  8.     <string name="setoff_light2"></string>
  9. </resources>
4,布局文件脚本main.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <TextView 
  8.     android:layout_width="fill_parent" 
  9.     android:layout_height="wrap_content" 
  10.     android:text="@string/hello"
  11.     />
  12.     <LinearLayout
  13.           android:layout_width="fill_parent"
  14.           android:layout_height="wrap_content"
  15.           android:orientation="horizontal" 
  16.           android:gravity="center">
  17.     
  18.      <Button
  19.          android:id="@+id/button_seton_light1"
  20.              android:layout_width = "wrap_content"
  21.              android:layout_height = "wrap_content"
  22.              android:text = "@string/seton_light1"
  23.      />
  24.      
  25.      <Button
  26.      android:id = "@+id/button_setoff_light1"
  27.          android:layout_width = "wrap_content"
  28.          android:layout_height = "wrap_content"
  29.          android:text = "@string/setoff_light1"
  30.      />
  31.      
  32.      <Button
  33.          android:id="@+id/button_seton_light2"
  34.              android:layout_width = "wrap_content"
  35.              android:layout_height = "wrap_content"
  36.              android:text = "@string/seton_light2"
  37.      />
  38.      
  39.      <Button
  40.      android:id = "@+id/button_setoff_light2"
  41.          android:layout_width = "wrap_content"
  42.          android:layout_height = "wrap_content"
  43.          android:text = "@string/setoff_light2"
  44.      />
  45.      </LinearLayout>
  46. </LinearLayout>
5,由于是编译器是Eclipse,但实际编译却是要在android源代码环境下编译,于是,将工程目录的三个文件
  src,res以及AndroidManifest.xml复制到gingerbread源码目录的package/experimental/swtled下,其中
  swtled文件是需要自己新建的,复制完成之后,进入编译阶段
 
6,编译app并生成apk文件
  (1) mmm package/experimental/swtled
  编译成功后,便可以在out/target/product/generic/system/app目录下看到swtled.apk文件
  (2) 重新打包系统镜像文件system.img
   make snod
 
7,开机测试
 
 
8,注意事项:
在工程中需要import两个包是
 import android.os.ISwtledService;
 import android.os.ServiceManager;

程序通过ServiceManager.getService("swtled")来获得SwtledService,
接着通过ISwtledService.Stub.asInterface函数转换为ISwtledService接口。
其中,服务名字“swtled”是系统启动时加载SwtledService时指定的,
而ISwtledService接口定义在android.os.ISwtledService中
需要注意的是,虽然用Eclipse编译这个应用,但是无法用Eclipse编译,最后还是需要将工程中res,src以及AndroidManifest.xml三个文件copy到gingerbread/package/experimental/swtled,swtled是自己再新建的文件夹, 然后使用 mmm package/experimental/swtled编译,如果正确,最后会生成.apk文件。
 
9,结束
以上四篇文章记录了在android平台下使用硬件led灯实现从java应用层控制灯的四个实现阶段,这个主要是记录自己的一些心得,在开发阶段也借鉴参考了一些网友的文章,在此表示感谢。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值