10使用VMware菜单"虚拟机/可移动设备|GoogleWS-SX-001连接(断开与主机的连接)",连接Android实验箱到Ubuntu系统。11单击AndroidStudio工具栏按钮"Run‘app或菜单“Run|Run‘app"运行程序,弹出"SelectDeploymentTarget"对话框,如下图所示。SelectDeploymerntTargeartednwBaxiab Ws-5X-001 (Android 4.0.3,API15)Available EmulatarsMyAvdACreateNewEmulatorDonitseevourdeviceise selectionforfuture launchesOKCancel12在"SelectDeploymentTarget"对话框中,选中"BoxlabWS-SX-001(Android4.0.3,API15)"项目,单击右下部OK"按钮,将程序下载到开发板运行。3.3Android服务1使用AndroidStudio创建项目应用程序名:HelloService(加学号作为后缀,后续操作凡是涉及HelloService都加后缀,不再赞述)。公司域名:com.example(默认)。包名:com.example.helloservice(默认,自动加上学号后缀)。应用程序存放位置:学号目录。Minimum SDK:API15:Android4.0.3(lceCreamSandwich)。活动:EmptyActivity。9
9 10 使用 VMware 菜单“虚拟机|可移动设备|Google WS-SX-001|连接(断开与主机的连接)”, 连接 Android 实验箱到 Ubuntu 系统。 11 单击 Android Studio 工具栏按钮“Run ‘app’”或菜单“Run|Run ‘app’”,运行程序,弹出“Select Deployment Target”对话框,如下图所示。 12 在“Select Deployment Target”对话框中,选中“Boxlab WS-SX-001 (Android 4.0.3, API 15)” 项目,单击右下部“OK”按钮,将程序下载到开发板运行。 3.3 Android 服务 1 使用 Android Studio 创建项目 应用程序名:HelloService(加学号作为后缀,后续操作凡是涉及 HelloService 都加后缀,不 再赘述)。 公司域名:com.example(默认)。 包名:com.example.helloservice(默认,自动加上学号后缀)。 应用程序存放位置:学号目录。 Minimum SDK: API 15: Android4.0.3 (IceCreamSandwich)。 活动:Empty Activity
活动名:MainActivity(默认)。布局名:activity_main(默认)。项目视图:project。如下图所示。axDnaaLE2创建ServiceDemo类在app/src/main/com.example.helloservice包上,单击鼠标右键,弹出快捷菜单,单击菜单项目“NewServiceService",弹出NewAndroidComponent"对话框,输入类名“ServiceDemo",如下图所示。单击下方“Finish"按钮,确认创建。configureComponentDEAFFesCanceEinish在ServiceDemo类(ServiceDemo.java)中输入如下代码,并如下图所示。注意:import语句不需要输入,AndroidStudio自动检测,按下“Alt+Enter”输入;后续实验类似处理,不再赞述。packagecom.example.helloservice;10
10 活动名:MainActivity(默认)。 布局名:activity_main(默认)。 项目视图:project。 如下图所示。 2 创建 ServiceDemo 类 在 app/src/main/com.example.helloservice 包上,单击鼠标右键,弹出快捷菜单,单击菜单项 目“New|Service|Service”,弹出“New Android Component”对话框,输入类名“ServiceDemo”, 如下图所示。单击下方“Finish”按钮,确认创建。 在 ServiceDemo 类(ServiceDemo.java)中输入如下代码,并如下图所示。 注意:import 语句不需要输入,Android Studio 自动检测,按下“Alt+Enter”输入;后续实 验类似处理,不再赘述。 package com.example.helloservice;
import android.app.Service;import android.content.Intent;import android.os.IBinder,import android.util.Log,publicclass ServiceDemoextends Serviceprivate static final String TAG="ServiceDemo";public static final String ACTION="com.example.helloservice.ServiceDemo";public ServiceDemo(7@Overridepublic void onCreateO Log.v(TAG, "ServiceDemo onCreate"),super.onCreateO;1@Overridepublic void onStart(Intent intent, int startId)Log.v(TAG, "ServiceDemo onStart");@Overridepublic int onStartCommand(Intent intent, int flags, int startld)Log.v(TAG,"ServiceDemoonStartCommand")returnsuper.onStartCommand(intent,flags,startld)1@Overridepublic void onDestroyO (Log.v(TAG,"ServiceDemo onDestroy"),super.onDestroyO;TLAOTFea11
11 import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class ServiceDemo extends Service { private static final String TAG = "ServiceDemo" ; public static final String ACTION = "com.example.helloservice.ServiceDemo"; public ServiceDemo() { } @Override public void onCreate() { Log.v(TAG, "ServiceDemo onCreate"); super.onCreate(); } @Override public void onStart(Intent intent, int startId) { Log.v(TAG, "ServiceDemo onStart"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.v(TAG, "ServiceDemo onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.v(TAG, "ServiceDemo onDestroy"); super.onDestroy(); } }
3修改AndroidManifest.xml文件修改app/src/main/AndroidManifest.xml文件如下,设置ServiceDemo服务的IntentFilter,并如下图所示。<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android=-"http:/schemas.android.com/apk/res/android"package="com.example.helloservice"><applicationandroid:allowBackup-"true"android:icon="@mipmap/ic_launcher"android:label-"@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=",MainActivity"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity><serviceandroid:name=".ServiceDemo"android:enabled-"true"android:exported="true"><intent-filter><actionandroid:name="com.example.helloservice.ServiceDemo"/></intent-filter></servicex</application></manifest>naa4修改activity_main.xml文件12
12 3 修改 AndroidManifest.xml 文件 修改 app/src/main/ AndroidManifest.xml 文件如下,设置 ServiceDemo 服务的 Intent Filter,并 如下图所示。 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloservice"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".ServiceDemo" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.helloservice.ServiceDemo"/> </intent-filter> </service> </application> </manifest> 4 修改 activity_main.xml 文件
双击AndroidStudio左侧app/src/main/res/layout/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="match_parent"android:layout height-"match _parent"><Buttonandroid:id=-"@+id/start"android:layout_width-"match_parent"android:layout_height-"wrap_content"android:text-"Start Service"><Buttonandroid:id="@+id/stop"android:layout_width="match_parent"android:layout_height-"wrap_content"android:text-"Stop Service"></LinearLayout>O5修改MainActivityjava文件双击AndroidStudio左侧app/src/main/java/com.example.helloservice/MainActivity.java文件,修改后的代码如下,并如下图所示。package com.example.helloservice:import android.content.Intent;importandroid.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log,import android.view.View,13
13 双击 Android Studio 左侧 app/src/main/res/layout/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="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Service" /> <Button android:id="@+id/stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service" /> </LinearLayout> 5 修改 MainActivity.java 文件 双击 Android Studio 左侧 app/src/main/java/com.example.helloservice/MainActivity.java 文件, 修改后的代码如下,并如下图所示。 package com.example.helloservice; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View;