在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务---Android8.0版本实现-对照老罗版本

老罗版本参见:https://blog.csdn.net/luoshengyang/article/details/6580267

 我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。

       完整代码见:链接:https://pan.baidu.com/s/1DOwrehjEXXiEPmU0DNMumw 
       提取码:2jho

        一. 参照在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务一文,在Application Frameworks层定义好自己的硬件服务HelloService,并提供IHelloService接口提供访问服务。

       二. 为了方便开发,我们可以在IDE环境下使用Android SDK来开发Android应用程序。开发完成后,再把程序源代码移植到Android源代码工程目录中。原文中使用Eclipse,现在几乎所有的项目都使用AndroidStudio了,因此我们是使用AndroidStudio,但是主要文件是一样的:

    主程序是Hello.java:

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);
    	}
    }
}

    程序通过ServiceManager.getService("hello")来获得HelloService,接着通过IHelloService.Stub.asInterface函数转换为IHelloService接口。其中,服务名字“hello”是系统启动时加载HelloService时指定的,而IHelloService接口定义在android.os.IHelloService中,具体可以参考在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务---Android8.0版本实现-对照老罗版本一文。这个程序提供了简单的读定自定义硬件有寄存器val的值的功能,通过IHelloService.getVal和IHelloService.setVal两个接口实现。

界面布局文件res/layout/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>

三. 将src目录下文件目录拷贝至packages/apps/Hello目录,新增Android.mk文件:
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 := Hello
LOCAL_CERTIFICATE := platform
include $(BUILD_PACKAGE)
四. 编译:
 zh@ubuntu:sourcecode/trunk/Android/packages/apps/Hello$ mma -j16
编译成功后,便可以在Android/out/target/product/Hi3571V/system/app目录下看到Hello.apk文件了。
五. 重新打包系统,系统中就有Hello.apk了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值