Android编程基础 实验报告材料三Word格式文档下载.docx

上传人:b****8 文档编号:22425721 上传时间:2023-02-04 格式:DOCX 页数:20 大小:159.38KB
下载 相关 举报
Android编程基础 实验报告材料三Word格式文档下载.docx_第1页
第1页 / 共20页
Android编程基础 实验报告材料三Word格式文档下载.docx_第2页
第2页 / 共20页
Android编程基础 实验报告材料三Word格式文档下载.docx_第3页
第3页 / 共20页
Android编程基础 实验报告材料三Word格式文档下载.docx_第4页
第4页 / 共20页
Android编程基础 实验报告材料三Word格式文档下载.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

Android编程基础 实验报告材料三Word格式文档下载.docx

《Android编程基础 实验报告材料三Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Android编程基础 实验报告材料三Word格式文档下载.docx(20页珍藏版)》请在冰豆网上搜索。

Android编程基础 实验报告材料三Word格式文档下载.docx

编程要点:

主界面的Activity命名为MainActivity;

启动新的Activity命名为UserLoginActivity;

分别使用显示启动和隐式启动的方式,启动新的Activity;

用户名中不能出现“@”符号,同时长度不超过12个字符;

密码使用密码文本显示方式,即显示为“******”,同时只能为数字;

返回的用户名和密码要以Toast的方式显示出来;

MainActivity和UserLoginActivity中各个生命周期的回调函数中要以Log.i方式显示日志信息。

2.编程实现

编程建立一个简单的进程内服务,实现比较两个整数大小的功能。

服务提供IntCompare(Int,Int)函数,输入两个整数,输出较大的整数。

提供两个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(){

Intentintent=newIntent("

com.example.test"

隐式启动"

}

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);

(3)onRestoreInstanceState()"

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

publicvoidonResume(){

super.onResume();

(4)onResume()"

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

publicvoidonSaveInstanceState(BundlesavedInstanceState){

super.onSaveInstanceState(savedInstanceState);

(5)onSaveInstanceState()"

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

publicvoidonRestart(){

super.onRestart();

(6)onRestart()"

@Override//在活动生命周期结束时被调用,用来保存持久的数据或释放占用的资源。

publicvoidonPause(){

super.onPause();

(7)onPause()"

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

publicvoidonStop(){

super.onStop();

(8)onStop()"

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

publicvoidonDestroy(){

super.onDestroy();

(9)onDestroy()"

}

UserLoginActivity,java

publicclassUserLoginActivityextendsActivity{

EditTextusername,password;

Buttonsubmit,reset;

protectedvoidonCreate(BundlesavedInstanceState){

//TODOAuto-generatedmethodstub

setContentView(R.layout.login);

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(){

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(){

setResult(RESULT_CANCELED,null);

finish();

2.详细工程结构及部分代码(显式和隐式都在其中):

(显式启动)

部分代码:

/**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));

}};

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(){

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("

Stringc2=bundle.getString("

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;

(隐式启动)结果:

 

privatebooleanisBound=false;

privateCompareServicecompareService;

inta=0,b=0;

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);

isBound){

IntentserviceIntent=newIntent(MainActivity.this,CompareService.class);

bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE);

isBound=true;

}

Stringc1=one.getText().toString();

Stringc2=two.getText().toString();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 其它考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1