Android编程基础 实验报告三.docx
《Android编程基础 实验报告三.docx》由会员分享,可在线阅读,更多相关《Android编程基础 实验报告三.docx(17页珍藏版)》请在冰豆网上搜索。
Android编程基础实验报告三
南昌航空大学实验报告
二0一三年一月六日
课程名称:
移动终端编程技术实验名称:
Android组件通信和后台服务
班级:
姓名:
同组人:
指导教师评定:
签名:
一、实验目的
了解使用Intent进行组件通信的原理,掌握使用Intent启动Activity的方法;
掌握获取Activity返回值的方法;了解Intent过滤器的原理与匹配机制;
了解Service的原理和用途;
掌握进程内服务的管理方法;
掌握服务的隐式启动和显式启动方法;
二、实验工具
Eclipse(MyEclipse)+ADT+Android2.2SDK;
三、实验题目
1.编程实现
功能描述:
主界面上有一个“登录”按钮,点击“登录”按钮后打开一个新的Activity;新的Activity上面有输入用户名和密码的控件,在用户关闭这个Activity后,将用户输入的用户名和密码传递到主界面中。
编程要点:
主界面的Activity命名为MainActivity;启动新的Activity命名为UserLoginActivity;
分别使用显示启动和隐式启动的方式,启动新的Activity;
用户名中不能出现“@”符号,同时长度不超过12个字符;密码使用密码文本显示方式,即显示为“******”,同时只能为数字;
返回的用户名和密码要以Toast的方式显示出来;
MainActivity和UserLoginActivity中各个生命周期的回调函数中要以Log.i方式显示日志信息。
2.编程实现
功能描述:
编程建立一个简单的进程内服务,实现比较两个整数大小的功能。
服务提供IntCompare(Int,Int)函数,输入两个整数,输出较大的整数。
编程要点:
主界面的Activity命名为MainActivity;提供两个EditText,分别输入两个整数;提供一个Botton,启动比较过程;提供一个TextView,显示较大的整数;
分别使用启动方式和绑定方式使用Service;
分别使用Handle和AsyncTask更新TextView中的内容。
四、实验步骤
1.详细工程结构及部分代码(显式和隐式都在其中):
结果:
代码:
MainActivity.java
publicclassMainActivityextendsActivity{
protectedintSUBACTIVITY1=1;
privatestaticStringTAG="MainActivity";
Buttonbutton1,button2;
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG,"
(1)onCreate()");
button1=(Button)this.findViewById(R.id.button1);
button2=(Button)this.findViewById(R.id.button2);
button1.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewv){
Intentintent=newIntent(MainActivity.this,UserLoginActivity.class);
Toast.makeText(getApplicationContext(),"显示启动",Toast.LENGTH_SHORT).show();
startActivityForResult(intent,SUBACTIVITY1);
}});
button2.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewv){
Intentintent=newIntent("com.example.test");
Toast.makeText(getApplicationContext(),"隐式启动",Toast.LENGTH_SHORT).show();
startActivityForResult(intent,SUBACTIVITY1);
}});
}
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
super.onActivityResult(requestCode,resultCode,data);
if(resultCode==RESULT_OK){
UriuriData=data.getData();
Toast.makeText(getApplicationContext(),uriData.toString(),
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"用户名和密码为空",Toast.LENGTH_SHORT)
.show();
}
}
@Override//可视生命周期开始时被调用,对用户界面进行必要的更改
publicvoidonStart(){
super.onStart();
Log.i(TAG,"
(2)onStart()");
}
@Override//在onStart()后被调用,用于恢复onSaveInstanceState()保存的用户界面信息
publicvoidonRestoreInstanceState(BundlesavedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG,"(3)onRestoreInstanceState()");
}
@Override//在活动生命周期开始时被调用,恢复被onPause()停止的用于界面更新的资源
publicvoidonResume(){
super.onResume();
Log.i(TAG,"(4)onResume()");
}
@Override//在onResume()后被调用,保存界面信息
publicvoidonSaveInstanceState(BundlesavedInstanceState){
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG,"(5)onSaveInstanceState()");
}
@Override//在重新进入可视生命周期前被调用,载入界面所需要的更改信息
publicvoidonRestart(){
super.onRestart();
Log.i(TAG,"(6)onRestart()");
}
@Override//在活动生命周期结束时被调用,用来保存持久的数据或释放占用的资源。
publicvoidonPause(){
super.onPause();
Log.i(TAG,"(7)onPause()");
}
@Override//在可视生命周期结束时被调用,一般用来保存持久的数据或释放占用的资源
publicvoidonStop(){
super.onStop();
Log.i(TAG,"(8)onStop()");
}
@Override//在完全生命周期结束时被调用,释放资源,包括线程、数据连接等
publicvoidonDestroy(){
super.onDestroy();
Log.i(TAG,"(9)onDestroy()");
}
}
UserLoginActivity,java
publicclassUserLoginActivityextendsActivity{
EditTextusername,password;
Buttonsubmit,reset;
privatestaticStringTAG="MainActivity";
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Log.i(TAG,"
(1)onCreate()");
username=(EditText)this.findViewById(R.id.username);
password=(EditText)this.findViewById(R.id.password);
submit=(Button)this.findViewById(R.id.submit);
reset=(Button)this.findViewById(R.id.reset);
username.setFocusable(true);
username.setOnKeyListener(newOnKeyListener(){
publicbooleanonKey(Viewv,intkeyCode,KeyEventevent){
//TODOAuto-generatedmethodstub
intunicodeChar=event.getUnicodeChar();
if(unicodeChar==64)
{
returntrue;
}
else
returnfalse;
}});
submit.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
Stringname=username.getText().toString();
Stringpass=password.getText().toString();
StringuriString="用户名:
"+name+"密码:
"+pass;
Uridata=Uri.parse(uriString);
Intentresult=newIntent(null,data);
setResult(RESULT_OK,result);
finish();
}
});
reset.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
setResult(RESULT_CANCELED,null);
finish();
}
});
}
@Override//可视生命周期开始时被调用,对用户界面进行必要的更改
publicvoidonStart(){
super.onStart();
Log.i(TAG,"
(2)onStart()");
}
@Override//在onStart()后被调用,用于恢复onSaveInstanceState()保存的用户界面信息
publicvoidonRestoreInstanceState(BundlesavedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
Log.i(TAG,"(3)onRestoreInstanceState()");
}
@Override//在活动生命周期开始时被调用,恢复被onPause()停止的用于界面更新的资源
publicvoidonResume(){
super.onResume();
Log.i(TAG,"(4)onResume()");
}
@Override//在onResume()后被调用,保存界面信息
publicvoidonSaveInstanceState(BundlesavedInstanceState){
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG,"(5)onSaveInstanceState()");
}
@Override//在重新进入可视生命周期前被调用,载入界面所需要的更改信息
publicvoidonRestart(){
super.onRestart();
Log.i(TAG,"(6)onRestart()");
}
@Override//在活动生命周期结束时被调用,用来保存持久的数据或释放占用的资源。
publicvoidonPause(){
super.onPause();
Log.i(TAG,"(7)onPause()");
}
@Override//在可视生命周期结束时被调用,一般用来保存持久的数据或释放占用的资源
publicvoidonStop(){
super.onStop();
Log.i(TAG,"(8)onStop()");
}
@Override//在完全生命周期结束时被调用,释放资源,包括线程、数据连接等
publicvoidonDestroy(){
super.onDestroy();
Log.i(TAG,"(9)onDestroy()");
}
}
2.详细工程结构及部分代码(显式和隐式都在其中):
(显式启动)
结果:
部分代码:
MainActivity.java
publicclassMainActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
publicstaticintmaxNum;
publicstaticHandlerhandler=newHandler();
privatestaticTextViewresult=null;
privatestaticButtoncompare=null;
privatestaticButtonreset=null;
privatestaticEditTextone=null;
privatestaticEditTexttwo=null;
publicstaticvoidUpdateGUI(intrefreshDouble){
maxNum=refreshDouble;
handler.post(RefreshLable);
}
privatestaticRunnableRefreshLable=newRunnable(){
publicvoidrun(){
result.setText(String.valueOf(maxNum));
}};
publicvoidonCreate(BundlesavedInstanceState){
finalBundlemybundle=newBundle();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finalIntentintent=newIntent(MainActivity.this,CompareService.class);
result=(EditText)findViewById(R.id.result);
compare=(Button)findViewById(Rpare);
reset=(Button)findViewById(R.id.reset);
one=(EditText)findViewById(R.id.one);
two=(EditText)findViewById(R.id.two);
compare.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
mybundle.putString("one",one.getText().toString());
mybundle.putString("two",two.getText().toString());
intent.putExtras(mybundle);
startService(intent);
}
});
reset.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
one.setText(null);
two.setText(null);
result.setText(null);
}
});
}
}
CompareService.java
publicclassCompareServiceextendsService{
privateThreadworkThread;
Bundlebundle=null;
intone=0,two=0;
publicvoidonCreate(){
super.onCreate();
workThread=newThread(null,backgroudWork,"WorkThread");
}
@Override
publicvoidonStart(Intentintent,intstartId){
super.onStart(intent,startId);
bundle=intent.getExtras();
Stringc1=bundle.getString("one");
Stringc2=bundle.getString("two");
if(!
c1.toString().equals("")&&!
c2.toString().equals("")){
one=Integer.parseInt(c1);
two=Integer.parseInt(c2);
}
if(!
workThread.isAlive()){
workThread.start();
}
}
publicIBinderonBind(Intentintent){
returnnull;
}
privateRunnablebackgroudWork=newRunnable(){
@Override
publicvoidrun(){
intrandomDouble=IntCompare(one,two);
MainActivity.UpdateGUI(randomDouble);
stopSelf();
}
};
intIntCompare(inta,intb){
if(a>=b)
returna;
else
returnb;
}
}
(隐式启动)结果:
部分代码:
MainActivity.java
publicclassMainActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
privatebooleanisBound=false;
privateCompareServicecompareService;
inta=0,b=0;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finalTextViewresult=(TextView)findViewById(R.id.result);
Buttoncompare=(Button)findViewById(Rpare);
Buttonreset=(Button)findViewById(R.id.reset);
finalEditTextone=(EditText)findViewById(R.id.one);
finalEditTexttwo=(EditText)findViewById(R.id.two);
if(!
isBound){
IntentserviceIntent=newIntent(MainActivity.this,CompareService.class);
bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE);
isBound=true;
}
compare.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
Stringc1=one.getText().toString();
Stringc2=two.getText().toString();
i