開發Android應用程式來使用硬體訪問服務

來源:互聯網
上載者:User

1、開發Android應用程式來使用硬體訪問服務

~/android-2.3_r1/packages/experimental/Freg

----AndroidManifest.java

----Android.mk

----src

----shy/luo/freg

----Freg.java

----res

----layout

----main.xml

----values

----string.xml

----drawable

----icon.png


Freg.java

package shy.luo.freg;import android.app.Activity;import android.os.ServiceManager;import android.os.Bundle;import android.os.IFregService;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 Freg extends Activity implements OnClickListener {private final static String LOG_TAG = "shy.luo.freg.FregActivity";private IFregService fregService = 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);fregService = IFregService.Stub.asInterface(ServiceManager.getService("freg"));                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, "Freg Activity Created");    }        @Override    public void onClick(View v) {    if(v.equals(readButton)) {try {    int val = fregService.getVal();    String text = String.valueOf(val);    valueText.setText(text);} catch (RemoteException e) {Log.e(LOG_TAG, "Remote Exception while reading value from freg service.");}    }    else if(v.equals(writeButton)) {try {    String text = valueText.getText().toString();    int val = Integer.parseInt(text);fregService.setVal(val);} catch (RemoteException e) {Log.e(LOG_TAG, "Remote Exception while writing value to freg service.");}    }    else if(v.equals(clearButton)) {    String text = "";    valueText.setText(text);    }    }}


main.xml

                                                         

strings.xml

    Freg    Value    Please input a value...    Read    Write    Clear

AndroidManifest.xml

                                                                                 

Android.mk

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_PACKAGE_NAME := Freginclude $(BUILD_PACKAGE)

2、編譯

編譯成apk:

產生的Freg.apk位於out/target/product/generic/system/app中。<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPiAgICAgILTysPyjujwvcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20140609/201406090910363.png" alt="\">

最後把Freg.apk重新打包進入system.img,位於out/target/product/gerneric中。


運行:



結果如下:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.