开发Android应用程序验证硬件访问服务
在框架层中开发硬件访问服务的目的就是为应用程序能够访问对应的硬件设备的。那么下面就来分析如何使用框架层中的对应的硬件访问服务,具体参见代码。
应用程序工程目录结构如下:
~Android/packages/experimental/Hello/Helloworld
---AndroidManifest.xml
---Android.mk
---src
---com.david.helloworld
---HelloWorld.java
---res
---layout
---main.xml
---values
---strings.xml
---drawable
---icon.png
Strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloWorld</string>
<string name="value">helloworld</string>
<string name="hint">input a value...</string>
<string name="get">Get</string>
<string name=”set">Set</string>
<string name="clear">clear</string>
</resources>
Main.xml;
<?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" >
<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"
>
<Button
android:id="@+id/button_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/get">
</Button>
<Button
android:id="@+id/button_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set">
</Button>
<Button
android:id="@+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear">
</Button>
</LinearLayout>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.david.helloworld"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloWorld"
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>
HelloWorld.java:
package com.david.helloworld;
import android.app.Activity;
import android.os.ServiceManager;
import android.os.Bundle;
import android.os.IHelloWorldService;
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 HelloWorld extends Activity implements OnClickListener {
private final static String LOG_TAG = "com.david.helloworld HelloWorldActivity";
private IHelloWorldService helloWorldService = null;
private EditText valueText = null;
private Button getButton = null;
private Button setButton = null;
private Button clearButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helloWorldService = IHelloWorldService.Stub.asInterface(
ServiceManager.getService("helloworld"));
valueText = (EditText)findViewById(R.id.edit_value);
getButton = (Button)findViewById(R.id.button_get);
setButton = (Button)findViewById(R.id.button_set);
clearButton = (Button)findViewById(R.id.button_clear);
getButton.setOnClickListener(this);
setButton.setOnClickListener(this);
clearButton.setOnClickListener(this);
Log.i(LOG_TAG, "Helloworld Activity Created");
}
@Override
public void onClick(View v) {
if(v.equals(getButton)) {
try {
int val = helloWorldService.getVal();
String text = String.valueOf(val);
valueText.setText(text);
} catch (RemoteException e) {
Log.e(LOG_TAG, "Remote Exception while reading value from helloworld service.");
}
}
else if(v.equals(setButton)) {
try {
String text = valueText.getText().toString();
int val = Integer.parseInt(text);
helloWorldService.setVal(val);
} catch (RemoteException e) {
Log.e(LOG_TAG, "Remote Exception while writing value to helloworld service.");
}
}
else if(v.equals(clearButton)) {
String text = "";
valueText.setText(text);
}
}
}
Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PACKAGE_NAME := HelloWorld
LOCAL_SRC_FILES := $(call all-subdir-java-files)
include $(BUILD_PACKAGE)
如果运行成功的话,会显示如下图片:
应用程序启动之后,我们可以通过点击get来从寄存器中获取值,点击set来往寄存器写入值,如果操作过程中,读取的值与写入的值相同的话,就表示我们成功的编写了硬件访问服务了。
写到这里,我相信各位已经对Android系统的体系结构和执行流程有比较深刻的理解了!在以后的文章中,我会继续更新文章,敬请期待!!!
本人刚创建了一个QQ群,目的是共同研究学习Android,期待兴趣相投的同学加入!!
群号:179914858