首頁(yè) 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當(dāng)貝市場(chǎng)

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計(jì)算器

綜合交流 / 評(píng)測(cè) / 活動(dòng)區(qū)

交流區(qū) | 測(cè)硬件 | 網(wǎng)站活動(dòng) | Z幣中心

新手入門 / 進(jìn)階 / 社區(qū)互助

新手 | 你問(wèn)我答 | 免費(fèi)刷機(jī)救磚 | ROM固件

查看: 31011|回復(fù): 0
上一主題 下一主題
[教程]

在Ubuntu上為Android系統(tǒng)內(nèi)置Java應(yīng)用程序測(cè)試Application Frameworks

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:30 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式 | 未知
我們?cè)贏ndroid系統(tǒng)增加硬件服務(wù)的目的是為了讓應(yīng)用層的APP能夠通過(guò)Java接口來(lái)訪問(wèn)硬件服務(wù)。那么, APP如何通過(guò)Java接口來(lái)訪問(wèn)Application Frameworks層提供的硬件服務(wù)呢?在這一篇文章中,我們將在Android系統(tǒng)的應(yīng)用層增加一個(gè)內(nèi)置的應(yīng)用程序,這個(gè)內(nèi)置的應(yīng)用程序通過(guò)ServiceManager接口獲取指定的服務(wù),然后通過(guò)這個(gè)服務(wù)來(lái)獲得硬件服務(wù)。   
   
        一. 參照在Ubuntu上為Android系統(tǒng)的Application Frameworks層增加硬件訪問(wèn)服務(wù)一文,在Application Frameworks層定義好自己的硬件服務(wù)HelloService,并提供IHelloService接口提供訪問(wèn)服務(wù)。   
   
       二. 為了方便開(kāi)發(fā),我們可以在IDE環(huán)境下使用Android SDK來(lái)開(kāi)發(fā)Android應(yīng)用程序。開(kāi)發(fā)完成后,再把程序源代碼移植到Android源代碼工程目錄中。使用Eclipse的Android插件ADT創(chuàng)建Android工程很方便,這里不述,可以參考網(wǎng)上其它資料。工程名稱為Hello,下面主例出主要文件:   
   
    主程序是src/shy/luo/hello/Hello.java:   
  1. package shy.luo.hello;   
       
    import shy.luo.hello.R;   
    import android.app.Activity;   
    import android.os.ServiceManager;   
    import android.os.Bundle;   
    import android.os.IHelloService;   
    import android.os.RemoteException;   
    import android.util.Log;   
    import android.view.View;   
    import android.view.View.OnClickListener;   
    import android.widget.Button;   
    import android.widget.EditText;   
       
    public class Hello extends Activity implements OnClickListener {   
            private final static String LOG_TAG = "shy.luo.renju.Hello";   
               
            private IHelloService helloService = null;   
       
            private EditText valueText = null;   
            private Button readButton = null;   
            private Button writeButton = null;   
            private Button clearButton = null;   
               
        /** Called when the activity is first created. */   
        @Override   
        public void onCreate(Bundle savedInstanceState) {   
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.main);   
       
            helloService = IHelloService.Stub.asInterface(   
                    ServiceManager.getService("hello"));   
               
            valueText = (EditText)findViewById(R.id.edit_value);   
            readButton = (Button)findViewById(R.id.button_read);   
            writeButton = (Button)findViewById(R.id.button_write);   
            clearButton = (Button)findViewById(R.id.button_clear);   
       
            readButton.setOnClickListener(this);   
            writeButton.setOnClickListener(this);   
            clearButton.setOnClickListener(this);   
               
            Log.i(LOG_TAG, "Hello Activity Created");   
        }   
          
        @Override   
        public void onClick(View v) {   
                if(v.equals(readButton)) {   
                    try {   
                                int val = helloService.getVal();   
                                String text = String.valueOf(val);   
                                valueText.setText(text);   
                    } catch (RemoteException e) {   
                            Log.e(LOG_TAG, "Remote Exception while reading value from device.");   
                    }                  
                }   
                else if(v.equals(writeButton)) {   
                    try {   
                                String text = valueText.getText().toString();   
                                int val = Integer.parseInt(text);   
                            helloService.setVal(val);   
                    } catch (RemoteException e) {   
                            Log.e(LOG_TAG, "Remote Exception while writing value to device.");   
                    }   
                }   
                else if(v.equals(clearButton)) {   
                        String text = "";   
                        valueText.setText(text);   
                }   
        }   
    }
復(fù)制代碼
程序通過(guò)ServiceManager.getService("hello")來(lái)獲得HelloService,接著通過(guò)IHelloService.Stub.asInterface函數(shù)轉(zhuǎn)換為IHelloService接口。其中,服務(wù)名字“hello”是系統(tǒng)啟動(dòng)時(shí)加載HelloService時(shí)指定的,而IHelloService接口定義在android.os.IHelloService中,具體可以參考在Ubuntu上為Android系統(tǒng)的Application Frameworks層增加硬件訪問(wèn)服務(wù)一文。這個(gè)程序提供了簡(jiǎn)單的讀定自定義硬件有寄存器val的值的功能,通過(guò)IHelloService.getVal和IHelloService.setVal兩個(gè)接口實(shí)現(xiàn)。   
   
界面布局文件res/layout/main.xml:   
  1. <?xml version="1.0" encoding="utf-8"?>   
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
           android:orientation="vertical"   
           android:layout_width="fill_parent"   
           android:layout_height="fill_parent">   
           <LinearLayout   
              android:layout_width="fill_parent"   
              android:layout_height="wrap_content"   
              android:orientation="vertical"   
              android:gravity="center">   
              <TextView   
                 android:layout_width="wrap_content"   
                 android:layout_height="wrap_content"   
                 android:text="@string/value">   
              </TextView>   
              <EditText   
                 android:layout_width="fill_parent"   
                 android:layout_height="wrap_content"   
                 android:id="@+id/edit_value"   
                 android:hint="@string/hint">   
              </EditText>   
           </LinearLayout>   
           <LinearLayout   
              android:layout_width="fill_parent"   
              android:layout_height="wrap_content"   
              android:orientation="horizontal"   
              android:gravity="center">   
              <Button   
                 android:id="@+id/button_read"   
                 android:layout_width="wrap_content"   
                 android:layout_height="wrap_content"   
                 android:text="@string/read">   
              </Button>   
              <Button   
                 android:id="@+id/button_write"   
                 android:layout_width="wrap_content"   
                 android:layout_height="wrap_content"   
                 android:text="@string/write">   
              </Button>   
              <Button   
                 android:id="@+id/button_clear"   
                 android:layout_width="wrap_content"   
                 android:layout_height="wrap_content"   
                 android:text="@string/clear">   
              </Button>   
           </LinearLayout>   
        </LinearLayout>
復(fù)制代碼
字符串文件res/values/strings.xml:   
  1. <?xml version="1.0" encoding="utf-8"?>   
        <resources>   
           <string name="app_name">Hello</string>   
           <string name="value">Value</string>   
           <string name="hint">Please input a value...</string>   
           <string name="read">Read</string>   
           <string name="write">Write</string>   
           <string name="clear">Clear</string>   
        </resources>   
復(fù)制代碼
程序描述文件AndroidManifest.xml:   
  1. <?xml version="1.0" encoding="utf-8"?>   
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
          package="shy.luo.hello"   
          android:versionCode="1"   
          android:versionName="1.0">   
          <application android:icon="@drawable/icon" android:label="@string/app_name">   
            <activity android:name=".Hello"   
                      android:label="@string/app_name">   
                <intent-filter>   
                    <action android:name="android.intent.action.MAIN" />   
                    <category android:name="android.intent.category.LAUNCHER" />   
                </intent-filter>   
            </activity>   
          </application>   
          <uses-sdk   
              android:minSdkVersion="7"   
              android:targetSdkVersion="7">   
          </uses-sdk>   
        </manifest>
復(fù)制代碼
三. 將Hello目錄拷貝至packages/experimental目錄,新增Android.mk文件:   
    USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk   
Android.mk的文件內(nèi)容如下:   
LOCAL_PATH:= $(call my-dir)   
include $(CLEAR_VARS)   
LOCAL_MODULE_TAGS := optional   
LOCAL_SRC_FILES := $(call all-subdir-java-files)   
LOCAL_PACKAGE_NAME := Hello   
include $(BUILD_PACKAGE)   
四. 編譯:   
USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Hello   
編譯成功后,便可以在out/target/product/generic/system/app目錄下看到Hello.apk文件了。   
    五. 重新打包系統(tǒng)鏡像文件system.img:   
USER-NAME@MACHINE-NAME:~/Android$ make snod   
    重新打包后的system.img文件就內(nèi)置了Hello.apk文件了。   
六. 運(yùn)行Android模擬器:   
USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel kernel/common/arch/arm/boot/zImage &   
在Home Screen中可以看到Hello應(yīng)用程序:   
     
   
打開(kāi)Hello應(yīng)用程序:   
   
     
點(diǎn)擊Read按鈕,可以從HelloService中讀取硬件寄存器val的值;點(diǎn)擊Clear按鈕,可以清空文本框的值;在文本框中輸入一個(gè)數(shù)值,再點(diǎn)擊Write按鈕,便可以將這個(gè)值寫入到硬件寄存器val中去,可以再次點(diǎn)擊Read按鈕來(lái)驗(yàn)證是否正確寫入了值。   
至此,我們就完整地學(xué)習(xí)了在Android的Linux內(nèi)核空間添加硬件驅(qū)動(dòng)程序、在Android的硬件抽象層添加硬件接口、在Android的Application Frameworks層提供硬件服務(wù)以及在Android的應(yīng)用層調(diào)用硬件服務(wù)的整個(gè)過(guò)程了,希望能為讀者進(jìn)入Android系統(tǒng)提供入門幫助。重新學(xué)習(xí)整個(gè)過(guò)程,請(qǐng)參考Android硬件抽象層(HAL)概要介紹和學(xué)習(xí)計(jì)劃

上一篇:Android日志系統(tǒng)驅(qū)動(dòng)程序Logger源代碼分析
下一篇:在Ubuntu上為Android系統(tǒng)的Application Frameworks層增加硬件訪問(wèn)服務(wù)
無(wú)效樓層,該帖已經(jīng)被刪除

本版積分規(guī)則

Archiver|新帖|標(biāo)簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號(hào) )

網(wǎng)絡(luò)信息服務(wù)信用承諾書 | 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號(hào)

GMT+8, 2024-10-20 05:32 , Processed in 0.067163 second(s), 16 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報(bào):report#znds.com (請(qǐng)將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表