智能终端软件开发实验
实验一:Android 用户界面(1)
一、实验目的
1. 熟悉Android 应用程序开发环境的构建过程,了解Android 应用程序的文件结构,学会进行Android 应用程序开发。
2. 理解Android 用户界面的基本概念,掌握TextView 、EditText 、Button 、ImageButton 、Checkbox 、RadioButton 、Spinner 、ListView 等常用控件的使用。
3. 了解Android 系统的6种界面布局,掌握线性布局、框架布局、表格布局、相对布局、绝对布局、网格布局等界面布局的使用。
二、实验内容
开发如图所示的Android 应用程序。
功能及要求。
1) 在EditText 中输入数据,当点击
把输入的信息在TestView 上显示;
2) 当选择CheckBox1时,在
示“CheckBox1被选中”;当选择CheckBox2
上显示“CheckBox2被选中”;当CheckBox1
被选中时,在TestView 上显示“CheckBox1
选中”;
3) 当选择RadioButton1时,在
示“RadioButton1被选中”;当选择
时,在TestView 上显示“RadioButton1被
RadioButton1和RadioButton2不能同时
4) 点击Close 时,关闭应用程序。 Input 按钮时,TestView 上显时,在TestView 和CheckBox2均和CheckBox2被TestView 上显RadioButton2选中”;被选中;
三、试做步骤和参考代码
1. 构建实验环境
1)下载adt-bundle-windows-x86-20130917.467161976(集成Android4.3版本);
2)安装JDK ;
3)注册环境变量。
2. 创建新的Android 程序过程
1)打开Android 工程向导:File →New →Project „︱Android →Android Project;
2)填写工程名称、选择程序运行的Android 系统版本、填写应用程序名称
3)点击“Finish ”按钮,工程向导会根据用户所填写的Android 工程信息,自动在后台创建Android 工程所需要的基础文件和目录结构。
3. 编程实现实验内容
【主要参考代码】
1)界面布局
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView01" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Null" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox01" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox02" >
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton01" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton02" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" >
2)获取各控件ID
final TextView textView = (TextView)findViewById(R.id.TextView01);
final EditText editText = (EditText )findViewById(R.id.EditText 01);
final CheckBox checkBox1= (CheckBox)findViewById(R.id.CheckBox01);
final CheckBox checkBox2= (CheckBox)findViewById(R.id.CheckBox02);
final RadioButton radioButton1 = (RadioButton)findViewById(R.id.RadioButton01); final RadioButton radioButton2 = (RadioButton)findViewById(R.id.RadioButton02); Button button1 = (Button)findViewById(R.id.Button01);
Button button2 = (Button)findViewById(R.id.Button02);
3)监听事件
CheckBox监听:
CheckBox.OnClickListener checkboxListener = new CheckBox.OnClickListener(){
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.CheckBox01:
textView.setText("CheckBox01,
isChecked:"+String.valueOf(checkBox1.isChecked()));
return;
case R.id.CheckBox02:
textView.setText("CheckBox02,
isChecked:"+String.valueOf(checkBox2.isChecked()));
return;
}
}};
RadioButton监听:
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;
case R.id.RadioButton02:
textView.setText("RadioButton2 is selected");;
return;
}
}};
radioButton1.setOnClickListener(radioButtonListener);
radioButton2.setOnClickListener(radioButtonListener);
Button监听:
Button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String msg = editText.getText().toString();
textView.setText(msg);
}
});
Button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
});
四、实验要求
1. 按时到指定实验室进行实验;
2. 学生应独立完成Android 程序开发平台的搭建;
3. 会创建新的Android 应用程序及编辑已创建的Android 应用程序;
4. 完成实验内容要求的Android 应用程序
五、实验报告
实验报告应包含以下内容:
1. 实验目的;
2. 实验内容;
3.Android 应用程序创建过程(工程名字用本人的姓或班内序号+Experiment1组成);
4. 给出主要的程序代码,如界面布局文件、逻辑代码文件等中的实现代码。
5. 在报告中给出程序运行界面;
6. 应有必要的总结和分析。
实验二:Android 用户界面(2)
一、实验目的
1. 掌握菜单资源的使用,能够编程实现选项菜单、子菜单和快捷菜单。
2. 了解操作栏和Fragment 的基本概念和使用方法。
3. 熟悉MVC 模型中的控制器概念和界面事件,能够处理单击事件、按键事件、触摸事件等界面事件的处理。
二、实验内容
开发如图所示的Android 应用程序。
(1) (2) (3)
功能及要求。
1) 图(1)所示为程序的初始界面,给出使用在XML 文件中定义和逻辑代码动态生成两种菜单定义方法的实现代码;
2) 定义菜单的的点击事件,该事件的处理过程是:若某个菜单子项被选中,在TextView 控件显示该菜单子项的名称和菜单ID ;
3) 图(2)所示是使用XML 文件方法实现菜单定义时,点击“打印”子项时的用户界面;
4) 图(3)所示是使用逻辑代码动态生成方法实现菜单定义时,点击“打印”子项时的用户界面。
三、试做步骤和参考代码
【主要参考代码】
1)XML 实现方法
2)获取各控件ID
final static int MENU_00 = Menu.FIRST;
final static int MENU_01 = Menu.FIRST+1;
final static int MENU_02 = Menu.FIRST+2;
final static int MENU_03 = Menu.FIRST+3;
final static int MENU_04 = Menu.FIRST+4;
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,MENU_00,0,"打印").setIcon(R.drawable.pic0);
menu.add(0,MENU_01,1,"新建").setIcon(R.drawable.pic1);
menu.add(0,MENU_02,2,"邮件").setIcon(R.drawable.pic2);
menu.add(0,MENU_03,3,"设置").setIcon(R.drawable.pic3);
menu.add(0,MENU_04,4,"订阅").setIcon(R.drawable.pic4);
return true;
}
3)逻辑代码
public boolean onOptionsItemSelected(MenuItem item) {
TextView label = (TextView)findViewById(R.id.label);
switch (item.getItemId()) {
case MENU_00:
label.setText("打印,菜单ID :" + item.getItemId());
return true;
case MENU_01:
label.setText("新建,菜单ID :" + item.getItemId());
return true;
case MENU_02:
label.setText("邮件,菜单ID :" + item.getItemId());
return true;
case MENU_03:
label.setText("设置,菜单ID :" + item.getItemId());
return true;
case MENU_04:
label.setText("订阅,菜单ID :" + item.getItemId());
return true;
default:
return false;
}
}
四、实验要求
1. 按时到指定实验室进行实验;
2. 完成实验内容要求的Android 应用程序
五、实验报告
实验报告应包含以下内容:
1. 实验目的;
2. 实验内容;
3.Android 应用程序创建过程(工程名字用本人的姓或班内序号+Experiment2组成);
4. 给出主要的程序代码,如界面布局文件、菜单定义文件、逻辑代码文件等中的实现代码。
5. 在报告中给出程序运行界面;
6. 应有必要的总结和分析。
实验三:Intent 及组件通信
一、实验目的
1. 了解使用Intent 进行组件通信的原理。
2. 掌握使用Intent 启动Activity 的方法。
3. 掌握获取Activity 返回值的方法。
二、实验内容
开发如图所示的Android 应用程序。
(1) (2) (3)
功能及要求。
1) 图(1)为程序的初始界面(父Activity ),其主要功能是提供1个TextView 和两个Button ,点击“启动Activity1”则启动图(2)所示的SubActivity1(子Activity ),点击“启动Activity2”则启动图(3)所示的SubActivity2(子Activity );
2) SubActivity1包含1个TextView 、1个EditText 和两个Button ,TextView 用于提示当前Activity 是子Activity ,EditText 用于输入返回父Activity 的信息,点击“接受”Button 则关闭SubActivity1并把输入信息返回,点击“撤销”Button 则直接关闭SubActivity1;
3) 当有信息从子Activity 返回时,父Activity 接收返回信息并把它显示在TextView 控件上;
4) SubActivity2包含1个TextView 、1个Button ,TextView 用于提示当前Activity 是子Activity ,Button 则直接关闭SubActivity2。
三、试做步骤和参考代码
【主要参考代码】
1)父Activity
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/textShow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Activity1"
/>
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Activity2"
/>
2)SubActivity1
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/textShow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SubActivity 1"
/>
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
android:id="@+id/btn_ok"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="接受"
/>
android:id="@+id/btn_cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="撤销"
/>
/>
3)SubActivity2
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/textShow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SubActivity 2"
/>
android:id="@+id/btn_return"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="关闭"
/>
4)子Activity 注册
android:name=".SubActivity1">
android:name=".SubActivity2">
4)SubActivity1定义
public class SubActivity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subactivity1);
final EditText editText = (EditText)findViewById(R.id.edit); Button btnOK = (Button)findViewById(R.id.btn_ok);
Button btnCancel = (Button)findViewById(R.id.btn_cancel);
btnOK.setOnClickListener(new OnClickListener(){
public void onClick(View view){
String uriString = editText.getText().toString(); Uri data = Uri.parse(uriString);
Intent result = new Intent(null, data);
setResult(RESULT_OK, result);
finish();
}
});
btnCancel.setOnClickListener(new OnClickListener(){ public void onClick(View view){
setResult(RESULT_CANCELED, null);
finish();
}
});
}
}
5)SubActivity2定义
public class SubActivity2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subactivity2);
Button btnReturn = (Button)findViewById(R.id.btn_return);
btnReturn.setOnClickListener(new OnClickListener(){
public void onClick(View view){
setResult(RESULT_CANCELED, null);
finish();
}
});
}
}
6)主要逻辑代码
public class ActivityCommunicationActivity extends Activity {
private static final int SUBACTIVITY1 = 1;
private static final int SUBACTIVITY2 = 2;
TextView textView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.textShow);
final Button btn1 = (Button)findViewById(R.id.btn1);
final Button btn2 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(ActivityCommunicationActivity.this, SubActivity1.class);
startActivityForResult(intent, SUBACTIVITY1);
}
});
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(ActivityCommunicationActivity.this, SubActivity2.class);
startActivityForResult(intent, SUBACTIVITY2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case SUBACTIVITY1:
if (resultCode == RESULT_OK){
Uri uriData = data.getData();
textView.setText(uriData.toString());
}
break;
case SUBACTIVITY2:
break;
}
}
}
四、实验要求
1. 按时到指定实验室进行实验;
2. 完成实验内容要求的Android 应用程序
五、实验报告
实验报告应包含以下内容:
1. 实验目的;
2. 实验内容;
3.Android 应用程序创建过程(工程名字用本人的姓或班内序号+Experiment3组成);
4. 给出主要的程序代码,如界面布局文件、菜单定义文件、逻辑代码文件等中的实现代码。
5. 在报告中给出程序运行界面;
6. 应有必要的总结和分析。
实验四:Service 及后台服务
一、实验目的
1. 了解Service 的原理和用途,掌握本地服务的管理方法。
2. 掌握服务的隐式启动和显式启动方法。。
3. 了解线程的启动、挂起和停止方法,了解跨线程的界面更新方法。
二、实验内容
开发如图所示的Android 应用程序。
(1) (2) (3)
功能及要求。
1)图(1)为程序的初始界面,设计4个TextView 和3个Button
2)点击“服务绑定”Button ,则将已定义好的“MathService ”服务绑定到本进程(“MathService ”服务的功能是提供“+、-、*、/”四个运算);
3)点击“取消绑定”Button ,则将 “MathService ”服务与本进程的绑定取消;
4)点击“数学运算”Button ,则首先生成两个0-100的随机数,然后分别计算这两个数的和、差、积和商,并分别显示在4个TextView 上;若点击“数学运算”Button 时,“MathService ”服务未被绑定或已经解除了绑定,则显示如图(3)所示信息。
三、试做步骤和参考代码
【主要参考代码】
1)界面设计
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="addResult">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="subResult">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="mulResult">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="divResult">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="服务绑定" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消绑定" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数学运算" >
2)服务定义
public class MathService extends Service{
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder{
MathService getService() {
return MathService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "本地绑定:MathService",
Toast.LENGTH_SHORT).show();
return mBinder;
}
@Override
public boolean onUnbind(Intent intent){
Toast.makeText(this, "取消本地绑定:MathService",
Toast.LENGTH_SHORT).show();
return false;
}
public long Add(long a, long b){
return a+b;
}
public long Sub(long a, long b){
return a-b;
}
public long Mul(long a, long b){
return a*b;
}
public double Div(long a, long b){
return (double)a/(double)b;
}
}
3)服务注册
4)主要逻辑代码
public class MainActivity extends Activity {
private MathService mathService;
private boolean isBound = false;
TextView addLable,subLable,mulLable,divLable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addLable = (TextView)findViewById(R.id.label1);
subLable = (TextView)findViewById(R.id.label2);
mulLable = (TextView)findViewById(R.id.label3);
divLable = (TextView)findViewById(R.id.label4);
Button bindButton = (Button)findViewById(R.id.bind);
Button unbindButton = (Button)findViewById(R.id.unbind);
Button computButton = (Button)findViewById(R.id.compute);
bindButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(!isBound){
final Intent serviceIntent = new Intent(MainActivity.this,MathService.class);
bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE); isBound = true;
}
}
});
unbindButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(isBound){
isBound = false;
unbindService(mConnection);
mathService = null;
}
}
});
computButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (mathService == null){
addLable.setText("未绑定服务");
subLable.setText("");
mulLable.setText("");
divLable.setText("");
return;
}
long a = Math.round(Math.random()*100);
long b = Math.round(Math.random()*100);
long addResult = mathService.Add(a, b);
String msg = String.valueOf(a)+" + "+String.valueOf(b)+
" = "+String.valueOf(addResult);
addLable.setText(msg);
long subResult = mathService.Sub(a, b);
msg = String.valueOf(a)+" - "+String.valueOf(b)+
" = "+String.valueOf(subResult);
subLable.setText(msg);
long mulResult = mathService.Mul(a, b);
msg = String.valueOf(a)+" * "+String.valueOf(b)+
" = "+String.valueOf(mulResult);
mulLable.setText(msg);
if(b == 0)
msg = "dividend cannot be 0 ";
else{
double divResult = mathService.Div(a, b);
msg = String.valueOf(a)+" / "+String.valueOf(b)+
" = "+String.valueOf(divResult);
}
divLable.setText(msg);
}
});
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mathService = ((MathService.LocalBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
mathService = null;
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
四、实验要求
1. 按时到指定实验室进行实验;
2. 完成实验内容要求的Android 应用程序
五、实验报告
实验报告应包含以下内容:
1. 实验目的;
2. 实验内容;
3.Android 应用程序创建过程(工程名字用本人的姓或班内序号+Experiment4组成);
4. 给出主要的程序代码,如界面布局文件、MathService 定义、服务注册、逻辑代码文件等的实现代码。
5. 在报告中给出程序运行界面;
6. 应有必要的总结和分析。