编写一个Android应用程序测试HelloService

本文介绍如何在Android应用层实现与底层服务交互的应用案例。通过创建一个简单的应用,演示如何使用getSystemService获取服务,并调用其方法完成数据读写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在经历了驱动层的窒息,Native层的缺氧,Framework层的束缚之后,我们终于来到了熟悉应用层,可以自由呼吸了。这篇文章的目的是写一个Android应用,通过getSystemService方法拿到HelloManager,然后调用readVal和writeVal方法,验证上篇文章写的JNI方法和服务能否正常运行。

打开Android Studio新建一个Android项目,Activity名字取为HelloActivity。通过上面的功能分析我们知道,我们需要一个EditText来让用户写入数据,需要两个按钮来执行读写方法,还需要一个TextView来显示读取的结果。所以布局文件如下

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.windcake.hello.HelloActivity">

    <EditText
        android:id="@+id/activity_hello_edittext"
        android:layout_width="match_parent"
        android:layout_height="40dp"/>

    <Button
        android:id="@+id/activity_hello_button_read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/read"
        />

    <Button
        android:id="@+id/activity_hello_button_write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/write"
        />

    <TextView
        android:id="@+id/activity_hello_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/read_value"
        />
</LinearLayout>

接下来写Activity的代码

首先初始化控件

private EditText mEditText;
private Button mReadButton;
private Button mWriteButton;
private TextView mTextView;
private void initView()
{
    mEditText = (EditText) findViewById(R.id.hello_activity_edittext);
    mReadButton = (Button) findViewById(R.id.hello_activity_button_read);
    mWriteButton = (Button) findViewById(R.id.hello_activity_button_write);
    mTextView = (TextView) findViewById(R.id.hello_activity_textview);

    mReadButton.setOnClickListener(this);
    mWriteButton.setOnClickListener(this);
}

然后写一个initHelloService方法拿到HelloManager的实例。

private HelloManager helloManager;
private void initHelloService()
{
  helloManager = (HelloManager)getSystemService(Context.HELLO_SERVICE);
}

将这两个方法放到onCreate方法中去调用一下。

准备工作做好之后,需要在onClick方法中进行逻辑操作。

@Override
public void onClick(View view)
{
    switch (view.getId())
    {
        case R.id.hello_activity_button_read:
            int val = helloManager.readVal();
            mTextView.setText("读取到的值为:" + val);
            break;
        case R.id.hello_activity_button_write:
            String s = mEditText.getText().toString();
            String s = mEditText.getText().toString();
            if (!TextUtils.isEmpty(s))
            {
              int wVal = Integer.parseInt(s);
              helloManager.writeVal(wVal);
            }
            break;
    }
}

在读取按钮的响应事件中,调用了HelloManager的readVal方法,去驱动里读值,并将读取到的值写到
TextView上。
在写入按钮的响应事件中,调用了HelloManager的writeVal方法,将值写入驱动,因为我们需要一个int型的值,所以对EditText所获取的内容进行了转换。

代码部分就写好了,接下来需要将这些代码编入系统中。

在/aosp/packages/apps目录下新建Hello文件夹。将Android Studio工程中的app/src/main文件夹下的java、res文件夹和AndroidManifest.xml文件拷贝进去,并将java文件夹重命名为src。新建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
include $(BUILD_PACKAGE)

为了让编译系统找到Hello模块,我们需要在/aosp/build/target/product目录下的generic_no_telephony.mk文件中增加Hello模块的信息。

PRODUCT_PACKAGES := \
    Bluetooth \
    BluetoothMidiService \
    Camera2 \
    Gallery2 \
    Music \
    MusicFX \
    OneTimeInitializer \
    Provision \
    SystemUI \
    WallpaperCropper \
    Hello

如此,重新编译Android源代码,并用虚拟机启动起来,就能在Laucher里看到Hello应用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值