第十一期 基于模拟器的Helloworld APP 访问硬件服务 《手机就是开发板》

        这一期我们来实现一个APP,从application层面来验证我们的HelloService服务。写APP还是得用IDE的开发环境来实现比较便捷,因为JAVA的开发环境不像C那样,使用VIM+各种插件就可以满足开发需要,所以建议使用android studio 进行开发,工程文件我放在https://github.com/aggresss/PHDemo.git 中的 Code目录下的 hello_APP 目录中,
或者直接访问 https://github.com/aggresss/PHDemo/tree/master/Code/hello_APP/Hello ,可把它直接COPY下来进行编译。
我这将APP的主要业务逻辑文件贴出来:
package com.example.phdemo.myapplication;

import android.os.RemoteException;
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 MainActivity extends Activity implements View.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.activity_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);
        }
    }
}
将Hello目录拷贝至packages/experimental目录,然后执行 mmm packages/experimental/Hello编译成功后,便可以在out/target/product/generic/system/app目录下看到Hello.apk文件了。
重新打包system.img文件,make snod。
然后启动模拟器 emulator -kernel kernel3.4/arch/arm/boot/zImage &
启动完成后在程序列表里就能看到一个 Hello 的APP ,打开应用程序后便可以验证APP与底层的驱动之间的通信是否正常。

        如果你执行过 make clean 这里需要注意两点:
1.有几个模块需要在make成功后重新单独编译:
mmm ./external/hello
mmm hardware/libhardware/modules/hello
mmm packages/experimental/Hello
因为这几个模块是独立的,不会编译进system.img文件中,所以我们执行完make 后在重新执行一遍上面三个命令,然后再 make snod 重新生成 system.img文件。
2.因为修改过framework文件,所以在make clean 再次编译会报错,这是执行 make update-api 后再进行 make 就能编译通过。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值