7. Android10内置app测试自定义service

本文是在上文6. Android10向系统注册service访问硬件抽象层(HAL)模块的基础上进行的,虚拟机环境就是上文结束时的环境。

创建app

首先在windows下使用Android studio创建Hello项目

修改MainActivity.java

package com.test.hello;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.IHelloService;
import android.os.ServiceManager;

public class MainActivity extends AppCompatActivity {
    final String  TAG = "hello!";
    Button btn_write = null;
    Button btn_read = null;
    TextView text_result = null;
    EditText text_input = null;
    private IHelloService helloService = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_write = (Button) findViewById(R.id.btn_w);
        btn_read = (Button) findViewById(R.id.btn_r);
        text_result = (TextView) findViewById(R.id.text_result);
        text_input = (EditText) findViewById(R.id.text_input);
        helloService = IHelloService.Stub.asInterface(
                ServiceManager.getService("hello"));

        btn_write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    helloService.wirteString(text_input.getText().toString());
                } catch (RemoteException e) {
                    Log.e(TAG, "Remote Exception while writing value to device.");
                }
            }
        });
        btn_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    text_result.setText(helloService.readString());
                } catch (RemoteException e) {
                    Log.e(TAG, "Remote Exception while writing value to device.");
                }
            }
        });
    }
}

 修改activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/text_input"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="something" />
            <Button
                android:id="@+id/btn_w"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="write" />
        </LinearLayout>


        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


            <TextView
                android:id="@+id/text_result"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:text="result" />
            <Button
                android:id="@+id/btn_r"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="read" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

将AndroidStudioProjects\Hello\app\src\main目录下的所有文件和文件夹打包,并移动到虚拟机中aosp10/packages/apps目录下新建的Hello文件夹去

 在aosp10/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_DEX_PREOPT := false
LOCAL_JACK_ENABLED := disabled
LOCAL_PRIVATE_PLATFORM_APIS := true

include $(BUILD_PACKAGE)

 编译Hello.apk

cd ~/Documents/aosp10
export TARGET_PREBUILT_KERNEL=/home/test/Documents/msm/arch/arm64/boot/Image.lz4-dtb
source build/envsetup.sh
lunch
aosp_walleye-userdebug
cd packages/apps/Hello/
mm

删掉这两个文件(packages/apps/Hello/res/values/themes.xml、packages/apps/Hello/res/values-night/themes.xml),重新编译 

mm

报错

 修改AndroidManifest.xml,删掉

        android:theme="@style/Theme.Hello"

修改前

修改后

 重新编译 

mm

编译完整镜像并重新刷机

编译,继续使用刚刚的terminal,先重新打包system.img

cd ../../..
make snod

 重新编译

time make -j16

报错

  解决

make api-stubs-docs-update-current-api
time make -j16

报错

解决

make system-api-stubs-docs-update-current-api
time make -j16

 刷机

sudo su
source build/envsetup.sh
lunch
aosp_walleye-userdebug
adb shell reboot bootloader
cd out/target/product/walleye/
fastboot flashall -w

 

测试

本文涉及到的知识主要和Android架构中的应用程序层、应用程序框架层相关

参考

Android应用程序访问linux驱动第四步:实现android应用程序_阳光玻璃杯-CSDN博客

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值