在经历了驱动层的窒息,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应用了。