Android从驱动到应用(7)_添加应用程序来使用硬件访问服务

前面已经搭建好硬件访问服务,下面来添加一个应用程序来使用下。
参考罗升阳的《Android系统源代码情景分析 [罗升阳著]》

目录结构如下:
在这里插入图片描述
包含一个源代码目录src、一个资源目录res、一个配置文件AndroidManifest.xml、和一个编译脚本Android.mk文件,下面看下具体的实现代码。
Demo.java:定义了一个Activity组件,他是应用程序的主界面,定义了一个编辑框和3个按钮,Read、Write、Clear

package shy.zpf.demo;

import android.app.Activity;
import android.os.ServiceManager;	
import android.os.Bundle;
import android.os.IDemoService;
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 Demo extends Activity implements OnClickListener {
    private final static String LOG_TAG = "shy.zpf.demo.DemoActivity";
    private IDemoService demoService = null;

    private EditText valueText = null;
    private Button readButton = null;
    private Button writeButton = null;
    private Button clearButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.main);
			demoService = IDemoService.Stub.asInterface(ServiceManager.getService("demo"));
			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, "Demo Activity Created");

	}

    @Override
    public void onClick(View v) {
		if(v.equals(readButton)) {
			try{
			int val = demoService.getVal();
			String text = String.valueOf(val);
			valueText.setText(text);

			}catch (RemoteException e) {
			   Log.e(LOG_TAG, "Remote Exception while reading value");			
			}
		} else if(v.equals(writeButton)) {
			try{
			String text = valueText.getText().toString();
			int val = Integer.parseInt(text);
			
			demoService.setVal(val);

			}catch (RemoteException e) {
			   Log.e(LOG_TAG, "Remote Exception while writing value");			
			}
		} else if(v.equals(clearButton)) {
			String text = ""; 
			valueText.setText(text);
		}

    }
}

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="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>

strings.xml:字符串资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Demo</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>

AndroidManifest.xml:应用程序配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="shy.zpf.demo"
	android:versionCode="1"
	android:versionName="1.0">

    <application android:label="@string/app_name"
        android:icon="@drawable/icon">
        <activity android:name=".Demo"
            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>
</manifest>

Android.mk文件

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
# Only compile source java files in this apk.
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Demo
include $(BUILD_PACKAGE)

编译打包:

mmm ./packages/experimental/Demo/
make snod
单刷systemimg镜像即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值