编写一个Android应用程序测试HelloService
2018-03-18 22:52
375 查看
在经历了驱动层的窒息,Native层的缺氧,Framework层的束缚之后,我们终于来到了熟悉应用层,可以自由呼吸了。这篇文章的目的是写一个Android应用,通过getSystemService方法拿到HelloManager,然后调用readVal和writeVal方法,验证上篇文章写的JNI方法和服务能否正常运行。
打开Android Studio新建一个Android项目,Activity名字取为HelloActivity。通过上面的功能分析我们知道,我们需要一个EditText来让用户写入数据,需要两个按钮来执行读写方法,还需要一个TextView来显示读取的结果。所以布局文件如下
然后写一个initHelloService方法拿到HelloManager的实例。
将这两个方法放到onCreate方法中去调用一下。
准备工作做好之后,需要在onClick方法中进行逻辑操作。
在读取按钮的响应事件中,调用了HelloManager的readVal方法,去驱动里读值,并将读取到的值写到
TextView上。
在写入按钮的响应事件中,调用了HelloManager的writeVal方法,将值写入驱动,因为我们需要一个int型的值,所以对EditText所获取的内容进行了转换。
为了让编译系统找到Hello模块,我们需要在/aosp/build/target/product目录下的generic_no_telephony.mk文件中增加Hello模块的信息。
如此,重新编译Android源代码,并用虚拟机启动起来,就能在Laucher里看到Hello应用了。
打开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应用了。
相关文章推荐
- 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景
- 编写一个单文档界面应用程序,该程序可以测试在鼠标左键按下时鼠标光标的位置是否处在某规定的矩形框内,如果不在该矩形内则计算机的扬声器会发出“叮”的声音,反之则会在用户区显示光标的位置。
- (1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”); (2)编写单元测试进行测试; (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
- 编写一个Android Linux内核驱动并用C可执行程序测试
- 使用 Bluemix 和 AppScan Mobile Analyzer 服务构建一个 Android 应用程序安全测试工具
- 使用MyEclipse Web Service来迅速开发和测试一个HelloWorld Web Service,
- 编写一个基本的Android应用程序
- (五)编写Android App测试Hello驱动程序API
- 编写一个基本的Android应用程序
- [Android Groovy]第二讲.编写一个Hello Groovy!的Android项目!!!
- 【转】编写一个基本的Android应用程序
- 一个hello程序的android内核模块编译方法及在模拟器中进行测试结论
- getSystemService()是Android很重要的一个API
- 关于Windows Service的一个编写技巧
- 利用C#编写一个简单的抓网页应用程序
- tiny210 hal 6 Android系统中编写APP通过应用程序框架层访问硬件服务。
- android如何确认一个应用程序的安装位置
- Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念?
- linux 下如何编写一个完整的应用程序!