打通驱动层到应用层--(四)应用层

前言

本节将编写JAVA程序,调用硬件服务,我使用的是IDEA编写JAVA工程,具体环境配置可自行搜索配置,

一、添加

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 := Helloxjq
include $(BUILD_PACKAGE)

二、修改

直接在IDEA新建的空白工程helloxjq上修改,
MainActivity.java

package com.example.helloxjq;

import android.os.Bundle;
import android.app.Activity;
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;

import android.os.ServiceManager;
import android.os.IHelloxjqService;
public class MainActivity extends Activity implements OnClickListener {
    private final static String LOG_TAG = "HELLOXJQ_JAVA";

    private IHelloxjqService HelloxjqService = 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);

        HelloxjqService = IHelloxjqService.Stub.asInterface(
                ServiceManager.getService("helloxjq"));

        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, "Helloxjq Activity Created");
    }

    @Override
    public void onClick(View v) {
        if(v.equals(readButton)) {
            try {
                valueText.setText(HelloxjqService.readString());
               /*  String r_val = HelloxjqService.readString();
                String text = String.valueOf(r_val);
                valueText.setText(text); */
            } catch (RemoteException e) {
                Log.e(LOG_TAG, "Remote Exception while reading value from device.");
            }
        }
        else if(v.equals(writeButton)) {
            try {
                HelloxjqService.wirteString(valueText.getText().toString());
 /*                String text = valueText.getText().toString();
                int w_val = Integer.parseInt(text);
                HelloxjqService.wirteString(text); */
            } catch (RemoteException e) {
                Log.e(LOG_TAG, "Remote Exception while writing value to device.");
            }
        }
        else if(v.equals(clearButton)) {
            String text = "";
            valueText.setText(text);
        }
    }
}

//public class MainActivity extends AppCompatActivity {
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//    }
//}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.helloxjq">
    <application android:icon="@mipmap/ic_launcher"
                 android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  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>

activity_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">
        <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="vertical"
            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">Helloxjq</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>

将新建的Android移入工程,且工程中仅保留以下文件
在这里插入图片描述

三、编译

删减完的工程不能使用IDEA编译,
将修改删减完的工程复制到packages/app/test/目录下
编译mmm packages/app/test/
打包make droidcore
重新下载代码即可看到手机上存在编写的helloxjqAPP,可以读取和写入驱动层添加的val节点,至此,全教程完。
有问题欢迎留言私信交流,非常感谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值