开发Android应用程序来使用硬件访问服务

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

<?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>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Freg</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.luo.freg"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Freg"
                  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

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := Freg

include $(BUILD_PACKAGE)

2、编译

编译成apk:

生成的Freg.apk位于out/target/product/generic/system/app中。

打包:

最后把Freg.apk重新打包进入system.img,位于out/target/product/gerneric中。

运行:

结果如下:

开发Android应用程序来使用硬件访问服务,布布扣,bubuko.com

时间: 2024-08-06 11:37:32

开发Android应用程序来使用硬件访问服务的相关文章

开发Android硬件访问服务

在http://blog.csdn.net/getnextwindow/article/details/47731597中,为Android系统添加了HAL模块,开发好一个硬件抽象层以后,我们通常还需要在应用程序框架中实现一个硬件访问服务.硬件访问服务通过硬件抽象层(HAL)模块来为应用程序提供读写操作.由于硬件抽象层使用c++开发,而应用程序框架中的硬件访问服务是Java语言开发的,因此,硬件访问服务必须通过JNI来调用抽象层模块的接口. Android系统的硬件访问服务通常运行在系统进程Sy

用 Eclipse 开发 Android 应用程序

转自:http://www.apkbus.com/android-13828-1-1.html 开始之前 本教程介绍如何在 Eclipse 环境中进行 Android 应用程序开发,包括两个示例应用程序.第一个示例是一个基本的应用程序,涉及构建和调试的所有阶段.第二个应用程序示例涉及比较复杂的 Android 特性,包括联系人搜索和 Google Maps 地址查找.要想从本教程获得最大收益,具备移动开发经验会有帮助,但不是必需的.开发 Android 应用程序需要 Java? 编程技能,但是对

Kotlin编程开发Android运用程序(Volley+Gson依赖库)

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> Kotlin编程开发Android运用程序(Volley+Gson依赖库) - hexingen的博客 - 博客频道 - CSDN.NET hexingen的博客 目录视图 摘要视图 订阅 [活动]2017 CSDN博客专栏评选 &nbsp [5月书讯]流畅的Pyth

使用jQuery Mobile和Phone Gap开发Android应用程序

经过了一段时间的学习,初步了解了该如何使用jQuery Mobile和 Phone Gap来开发一个Android应用程序,也想把这些东西介绍给大家. 1. 软件准备 要进行android app的开发,当然需要准备Java, eclipse和安装Android SDK,这个部分网络上面很多方法,搜索"安装Android SDK"即可找到很多答案,所以就不再这里浪费口水. 2. 知识准备 (1)了解jQuery Mobile这个js框架,知道怎么组织一个简单的页面. 官方网站:http

开发Android Map程序 如何获取 apikey (Google Map API v2)

1. 在Eclise --> preference --> android --> bulid 能够看到缺省的debug keystore:注意,最新版本的Android Eclipse中无需再自己产生MD5 和 SHA1 2. 登录Google网站账户后转到如下网址 https://console.developers.google.com/project/apps~soft-edu/apiui/api?authuser=0:在该页面中点击APIs然后将Google Maps Andr

使用jQuery Mobile和Phone Gap开发Android应用程序(转)

经过了一段时间的学习,初步了解了该如何使用jQuery Mobile和 Phone Gap来开发一个Android应用程序,也想把这些东西介绍给大家. 1. 软件准备 要进行android app的开发,当然需要准备Java, eclipse和安装Android SDK,这个部分网络上面很多方法,搜索“安装Android SDK”即可找到很多答案,所以就不再这里浪费口水. 2. 知识准备 (1)了解jQuery Mobile这个js框架,知道怎么组织一个简单的页面. 官方网站:http://jq

C#使用Xamarin开发Android应用程序 -- 系列文章

Xamarin开发Android应用程序 利用Xamaria构建Android应用-公交发车信息屏 Xamarin版的C# SVG路径解析器 C#使用Xamarin开发可移植移动应用(1.入门与Xamarin.Forms页面),附源码 为 Xamarin.Forms 做个跑马灯控件 [Xamarin挖墙脚系列:现有IPhone/IPad 设备尺寸] [Xamarin挖墙脚系列:IOS-关于手机支持的屏幕方向] [Xamarin挖墙脚系列:Xamarin.IOS机制原理剖析] [Xamarin挖墙

实现硬件访问服务的JNI方法

1.实现硬件访问服务的JNI方法 ~/android-2.3_r1/frameworks/base/services/jni ----com_android_server_FregService.cpp ----onload.cpp ----Android.mk com_android_server_FregService.cpp #define LOG_TAG "FregServiceJNI" #include "jni.h" #include "JNI

实现硬件访问服务

一. 1.定义硬件访问服务接口(为了进程间通信) ~/android-2.3_r1/frameworks/base ----Android.mk ----/core/java/android/os/IFregService.aidl Android系统提供了一种描述语言来定义具有跨进程访问能力的服务接口,这种描述语言称为Android接口描述语言(AIDL). IFregService.aidl package android.os; interface IFregService { void