《Android系统程序开发》实验指导书

更新时间:2023-07-17 15:32:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

《Android系统程序开发》

实验指导书

主编:邢海涛

集美大学

2015年2月

目录

实验一 Android开发环境 .............................................................................................................. 3 实验二 Android应用程序 .............................................................................................................. 6 实验三 Android生命周期 ............................................................................................................ 11 实验四 Android用户界面 ............................................................................................................ 14 实验五 组件通信与广播 ............................................................................................................... 26 实验六 后台服务 ........................................................................................................................... 39 实验七 数据存储与访问 ............................................................................................................... 53 实验八 位置服务于地图应用 ....................................................................................................... 77 实验九 Android NDK开发 ........................................................................................................... 84 实验十 综合示例设计与开发 ....................................................................................................... 88

实验一 Android开发环境

一、实验目的

1.掌握Android开发环境的安装配置方法。 2.掌握Android SDK安装和配置。 3.掌握Android模拟器的使用。

二、实验内容

搭建Android应用程序的开发环境。 三、实验仪器、设备

硬件:PC 微型计算机、1G以上内存,40G以上硬盘 软件:Windows XP,Eclipse , JDK , Android SDK

四、实验步骤

1.安装JDK和Eclipse

安装JDK,假设安装目录:C:\Program Files\Java\jdk1.6.0_14\

安装后,配置环境变量。 安装Eclipse

选择下载:Eclipse IDE for Java Developers (92MB),解压到相应的目录中。 启动Eclipse

双击目录中的eclipse.exe,假设选择工作目录:E:\Android\workplace,建议选择复选框,将工作目录设成默认工作目录。

2.安装Android SDK

Android开发站点:/android,点击“下载SDK”

选择Windows版本的Android SDK,文件名:android-sdk-windows-1.5_r3.zip,Android SDK只需解压,无需安装。解压目录:E:\Android\android-sdk-windows-1.5_r3(假定)。

3、安装ADT插件 安装

ADT

有两种方法。手动下载

ADT

插件,下载网址:

或Android SDK帮助文档中的下载页面,下载页面位置:<Android SDK>/docs/sdk/adt_download.html,选择下载:ADT_0.9.1.zip。

安装插件:启动Eclipse.选择Help → Install New Software,打开Eclipse插件的安装界面。点击“Add”。

点击Archive 选择ADT保存位置

选择ADT插件安装选项:复选Android DDMS和Android Development Tools

认可ADT插件的许可证 提示重启Eclipse,即完成。 自动下载ADT插件

自动下载ADT插件的方法和手动方法安装相似,不同之处在于:直接输入插件压缩包的下载地址/android/eclipse/。 设置Android SDK的保存路径

选择:Windows→Preferences 打开Eclipse的配置界面,输入Android SDK 的保存路径,点击“Apply”。

五、实验报告要求

1.严格按照实验报告格式完成实验报告并上交服务器。 2.按照步骤对实验过程进行截图和分析。

六、实验注意事项

注意将各实验步骤进行截图保存。

七、思考题

如何测试你的Android 应用程序开发环境已经搭建成功?

实验二 Android应用程序

一、实验目的

1. 掌握使用Eclipse开发Android应用程序的步骤。 2. 掌握Genymotion模拟器的创建和使用方法。 3. 了解Android应用程序结构。

二、实验内容

1. 建立一个HelloAndroid第一个Android程序。 2. 在Genymotion上运行HelloAndroid程序。

三、实验仪器、设备

硬件:PC 微型计算机、1G以上内存,40G以上硬盘 软件:Windows XP,Eclipse , JDK , Android SDK,ADT

四、实验步骤

1、建立HelloAndroid的步骤 1)创建Android工程

打开Android工程向导:File→New→Android Application Project或File→Project | Android→Android Application Project 或 File→New→Other…| Android→Android Application Project 。

填写应用名称(Application Name):HelloAndroid。自动生成工程名称(Project Name):HelloWorld与包名(Package Name):com.example.helloworld。修改Project Name,保证唯一性。

选择最小需求SDK版本(Minimum Required SDK),目标SDK版本(Target SDK),编译SDK版本(Compiled With)。

选择主题(Theme),点击Next。

勾选Create custom launcher icon,Create Activity,Create Project in WorkPlace,点击Next 设置启动图标(Configure Launcher Icon),点击Next。 勾选Create Activity,选择Blank Activity,点击Next。

填写Activity Name:HelloAndroid,Layout Name:activity_hello_android,Navigation Type:None,点击Finish。

2)代码实现如下:

HelloAndroid.java文件代码: package com.example.androidhello; import android.os.Bundle; import android.app.Activity; import android.view.Menu;

public class HelloAndroid extends Activity { @Override

protected void onCreate(Bundle savedInstanceState) { super setContentView(yout.activity_hello_android); } @Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.hello_android, menu); return true; } }

R.java文件如下:

package com.example.androidhello; public final class R {

public static final class attr { }

public static final class dimen {

public static final int activity_horizontal_margin=0x7f040000; public static final int activity_vertical_margin=0x7f040001; }

public static final class drawable {

public static final int ic_launcher=0x7f020000; }

public static final class id {

public static final int action_settings=0x7f080000; }

public static final class layout {

public static final int activity_hello_android=0x7f030000; }

public static final class menu {

public static final int hello_android=0x7f070000; }

public static final class string {

public static final int action_settings=0x7f050001; public static final int app_name=0x7f050000; public static final int hello_world=0x7f050002; }

public static final class style {

public static final int AppBaseTheme=0x7f060000; public static final int AppTheme=0x7f060001; } }

activity_hello_android.xml文件的代码:

<RelativeLayout xmlns:android="/apk/res/android" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".HelloAndroid" >

<TextView

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>

Strings.xml文件的代码:

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="app_name">AndroidHello</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> </resources>

AndroidManifest.xml文件的代码:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="/apk/res/android" package="com.example.androidhello" android:versionCode="1" android:versionName="1.0" > <uses-sdk

android:minSdkVersion="8" android:targetSdkVersion="18" /> <application

android:allowBackup="true"

android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity

android:name="com.example.androidhello.HelloAndroid" android:label="@string/app_name" > <intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="UNCHER" /> </intent-filter> </activity> </application> </manifest>

五、实验报告要求

1、上机输入和调试自己所编的程序。一人一组,独立上机调试,上机时出现的问题,最好独立解决。

2、严格按照实验报告格式完成实验报告。

六、实验注意事项

注意将各实验步骤进行截图保存。

七、思考题

1. 如何编写一个Android的应用程序,R.java和AndroidMainifest.xml文件有什么用途? 2. 如何引用资源

实验三 Android生命周期

一、实验目的

1. 巩固使用Eclipse开发Android应用程序方法和步骤。 2.加深了解和体验Activity的生命周期中各状态的变化关系。 3.掌握Android应用程序的调试方法和工具。

二、实验内容

1.建立在Android生命周期各阶段添加日志点的Android工程,工程名称:ActivityLifeCycle,包名称:edu.hrbeu.ActivityLifeCycle,Activity名称:ActivityLifeCycle。

三、实验仪器、设备

硬件:PC 微型计算机、1G以上内存,40G以上硬盘 软件:Windows XP,Eclipse , JDK , Android SDK

四、实验步骤

1. 建立工程请参考实验二。 具体代码如下:

ActivityLifeCycle.java文件的代码:

1. 2. 3. 4. 5. 6. 7. 8. 9.

package edu.hrbeu.ActivityLifeCycle;

import android.app.Activity; import android.os.Bundle; import android.util.Log;

public class ActivityLifeCycle extends Activity { private static String TAG = "LIFTCYCLE";

@Override //完全生命周期开始时被调用,初始化Activity

10. public void onCreate(Bundle savedInstanceState) { 11. super.onCreate(savedInstanceState); 12. setContentView(yout.main); 13. Log.i(TAG, "(1) onCreate()"); 14. } 15.

16. @Override //可视生命周期开始时被调用,对用户界面进行必要的更改

17. public void onStart() { 18. super.onStart();

19. Log.i(TAG, "(2) onStart()"); 20. } 21.

22. @Override //在

onStart()后被调用,用于恢复

onSaveInstanceState()保存的用户界面信息

23. public void onRestoreInstanceState(Bundle savedInstanceState) {

24. super.onRestoreInstanceState(savedInstanceState); 25. Log.i(TAG, "(3) onRestoreInstanceState()"); 26. } 27.

28. @Override //在活动生命周期开始时被调用,恢复被onPause()停止的用于界面更新的资源

29. public void onResume() { 30. super.onResume();

31. Log.i(TAG, "(4) onResume()"); 32. } 33.

34. @Override // 在onResume()后被调用,保存界面信息

35. public void onSaveInstanceState(Bundle savedInstanceState) { 36. super.onSaveInstanceState(savedInstanceState); 37. Log.i(TAG, "(5) onSaveInstanceState()"); 38. } 39.

40. @Override //在重新进入可视生命周期前被调用,载入界面所需要的更改信息

41. public void onRestart() { 42. super.onRestart();

43. Log.i(TAG, "(6) onRestart()"); 44. } 45.

46. @Override //在活动生命周期结束时被调用,用来保存持久的数据

或释放占用的资源

47. public void onPause() { 48. super.onPause();

49. Log.i(TAG, “(7) onPause()”);、 50. }

51.

52. @Override //在可视生命周期结束时被调用,一般用来保存持久的数据或释放占用的资源

53. public void onStop() { 54. super.onStop();

55. Log.i(TAG, "(8) onStop()"); 56. } 57.

58. @Override //在完全生命周期结束时被调用,释放资源,包括线程、数据连接等

59. public void onDestroy() { 60. super.onDestroy();

61. Log.i(TAG, "(9) onDestroy()"); 62. } 63. }

五、实验报告要求

1、上机输入和调试自己所编的程序。一人一组,独立上机调试,上机时出现的问题,最好独立解决。

2、严格按照实验报告格式完成实验报告并上交服务器

六、实验注意事项

注意将各实验步骤进行截图保存。

七、思考题

1. LogCat工具如何使用? 2.DevTools工具如何使用?

实验四 Android用户界面

一、实验目的

1. 加深对用户界面的控件的理解和使用。 2.掌握各种界面布局的用法。

3.掌握选项菜单、子菜单和快捷菜单的使用方法。

二、实验内容

1.

建立一个“ButtonDemo”的程序,包含Button和ImageButton两个按钮,上方是

“Button按钮”,下方是一个ImageButton控件?

2.

建立一个“CheckboxRadiobuttonDemo”程序,包含五个控件,从上至下分别是

TextView01、CheckBox01、 CheckBox02、RadioButton01、RadioButton02,当选择RadioButton01, RadioButton02则无法选择?

3.建立一个“TabDemo”程序,包含三个XML文件,分别为tab1.xml、tab2.xml和tab3.xml,这3个文件分别使用线性布局、相对布局和绝对布局示例中的main.xml的代码,并将布局的ID分别定义为layout01、layout02和layout03?

4.建立一个“MyContextMenu”程序,来完成Android菜单的功能。

三、实验仪器、设备

硬件:PC 微型计算机、1G以上内存,40G以上硬盘 软件:Windows XP,Eclipse , JDK , Android SDK

四、实验步骤

1. “ButtonDemo”源代码如下: ButtonDemo.java文件源如下:

package edu.hrbeu.ButtonDemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView;

public class ButtonDemo extends Activity {

/** Called when the activity is first created. */ @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(yout.main);

Button button = (Button)findViewById(R.id.Button01); ImageButton imageButton =

(ImageButton)findViewById(R.id.ImageButton01); button.setText("Button按钮");

imageButton.setImageResource(R.drawable.download);

final TextView textView = (TextView)findViewById(R.id.TextView01);

//(1)按钮注册到各自的监听器

button.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { textView.setText("Button按钮"); } });

imageButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { textView.setText("ImageButton按钮"); } });

//(2)按钮注册到同一个监听器

// Button.OnClickListener buttonListener = new Button.OnClickListener(){ // // // // // // // // // // // // // }

@Override

public void onClick(View v) { }};

switch(v.getId()){ }

case R.id.Button01:

textView.setText("Button按钮"); return;

textView.setText("ImageButton按钮"); return;

case R.id.ImageButton01:

//

button.setOnClickListener(buttonListener); imageButton.setOnClickListener(buttonListener);

}

Main.xml源代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/>

<Button android:id="@+id/Button01"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button01" >

</Button>

<ImageButton android:id="@+id/ImageButton01"

android:layout_width="wrap_content" android:layout_height="wrap_content">

</ImageButton>

</LinearLayout>

String.xml源代码如下:

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="hello">Hello World, ButtonDemo!</string> <string name="app_name">ButtonDemo</string> </resources>

AndroidManifest.xml文件源代码如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="/apk/res/android" package="edu.hrbeu.ButtonDemo" android:versionCode="1" android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".ButtonDemo" android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="UNCHER" /> </intent-filter> </activity> </application>

<uses-sdk android:minSdkVersion="3" />

</manifest>

2.“CheckboxRadiobuttonDemo”源代码如下: CheckboxRadiobuttonDemo.java文件的源代码如下:

package edu.hrbu.CheckboxRadiobuttonDemo;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.RadioButton; import android.widget.TextView;

public class CheckboxRadiobuttonDemo extends Activity { /** Called when the activity is first created. */ @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(yout.main);

final TextView textView = (TextView)findViewById(R.id.TextView01);

//(1)CheckBox

final CheckBox checkBox1= (CheckBox)findViewById(R.id.CheckBox01); final CheckBox checkBox2= (CheckBox)findViewById(R.id.CheckBox02); CheckBox.OnClickListener checkboxListener = new CheckBox.OnClickListener(){

@Override

public void onClick(View v) { }

switch(v.getId()){ case R.id.CheckBox01:

textView.setText("CheckBox01, return;

textView.setText("CheckBox02, return;

isChecked:"+String.valueOf(checkBox1.isChecked()));

case R.id.CheckBox02:

isChecked:"+String.valueOf(checkBox2.isChecked()));

}};

checkBox1.setOnClickListener(checkboxListener); checkBox2.setOnClickListener(checkboxListener);

//(2)RadioButton

final RadioButton radioButton1 =

(RadioButton)findViewById(R.id.RadioButton01); final RadioButton radioButton2 = (RadioButton)findViewById(R.id.RadioButton02);

RadioButton.OnClickListener radioButtonListener = new RadioButton.OnClickListener(){ }

@Override

public void onClick(View v) { }

switch(v.getId()){ case R.id.RadioButton01:

textView.setText("RadioButton1 is selected"); return;

textView.setText("RadioButton2 is selected");; return;

case R.id.RadioButton02:

}};

radioButton1.setOnClickListener(radioButtonListener); radioButton2.setOnClickListener(radioButtonListener);

}

Main.xml文件的源代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<TextView android:id="@+id/TextView01"

android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/>

<CheckBox android:id="@+id/CheckBox01"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox01" >

</CheckBox>

<CheckBox android:id="@+id/CheckBox02"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CheckBox02" >

</CheckBox>

<RadioGroup android:id="@+id/RadioGroup01"

android:layout_width="wrap_content" android:layout_height="wrap_content">

<RadioButton android:id="@+id/RadioButton01"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton01" >

</RadioButton>

<RadioButton android:id="@+id/RadioButton02"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton02" >

</RadioButton>

</RadioGroup>

</LinearLayout>

String.xml文件的源代码如下:

<?xml version="1.0" encoding="utf-8"?> <resources>

<string name="hello">Hello World, CheckboxRadiobuttonDemo!</string> <string name="app_name">CheckboxRadiobuttonDemo</string> </resources>

Androidmanifest.xml文件的源代码如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="/apk/res/android" package="edu.hrbu.CheckboxRadiobuttonDemo" android:versionCode="1" android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".CheckboxRadiobuttonDemo" android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="UNCHER" /> </intent-filter> </activity> </application>

<uses-sdk android:minSdkVersion="3" /> </manifest>

3.“TabDemo”源代码如下: TabDemo.java文件源代码如下:

package edu.hrbeu.TabDemo;

import android.app.TabActivity;

import android.os.Bundle; import android.widget.TabHost; import youtInflater;

public class TabDemo extends TabActivity {

/** Called when the activity is first created. */ @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

TabHost tabHost = getTabHost();

LayoutInflater.from(this).inflate(yout.tab1, tabHost.getTabContentView(),true);

LayoutInflater.from(this).inflate(yout.tab2, tabHost.getTabContentView(),true);

LayoutInflater.from(this).inflate(yout.tab3, tabHost.getTabContentView(),true);

tabHost.addTab(tabHost.newTabSpec("TAB1").

} }

setIndicator("线性布局").setContent(yout01)); setIndicator("绝对布局").setContent(yout02)); setIndicator("相对布局").setContent(yout03));

tabHost.addTab(tabHost.newTabSpec("TAB2"). tabHost.addTab(tabHost.newTabSpec("TAB3").

Tab1.xml文件的源代码如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id = "@+id/layout01"

xmlns:android="/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">

<TextView android:id="@+id/label"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" >

</TextView>

<EditText android:id="@+id/entry"

android:layout_height="wrap_content" android:layout_width="fill_parent">

</EditText>

<Button android:id="@+id/ok"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认:">

</Button>

<Button android:id="@+id/cancel"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消:" >

</Button>

</LinearLayout>

Tab2源代码如下:

<?xml version="1.0" encoding="utf-8" ?> <AbsoluteLayout android:id="@+id/layout02"

android:layout_width="fill_parent" android:layout_height="fill_parent"

xmlns:android="/apk/res/android"> <TextView android:id="@+id/label"

android:layout_x="40dip" android:layout_y="40dip"

android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/entry"

android:layout_x="40dip" android:layout_y="60dip"

android:layout_height="wrap_content" android:layout_width="150dip" />

<Button android:id="@+id/ok"

android:layout_width="70dip"

android:layout_height="wrap_content" android:layout_x="40dip" android:layout_y="120dip" android:text="确认" />

<Button android:id="@+id/cancel"

android:layout_width="70dip"

android:layout_height="wrap_content" android:layout_x="120dip" android:layout_y="120dip" android:text="取消" />

本文来源:https://www.bwwdw.com/article/6ga1.html

Top