智能終端軟件開發(fā)實(shí)驗(yàn)_第1頁(yè)
智能終端軟件開發(fā)實(shí)驗(yàn)_第2頁(yè)
智能終端軟件開發(fā)實(shí)驗(yàn)_第3頁(yè)
智能終端軟件開發(fā)實(shí)驗(yàn)_第4頁(yè)
智能終端軟件開發(fā)實(shí)驗(yàn)_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

實(shí)驗(yàn)一:Android顧客界面(1)

一、實(shí)驗(yàn)?zāi)康?/p>

1.熟悉Android應(yīng)用程序開發(fā)環(huán)境的構(gòu)建過程,理解Android應(yīng)用程序的文獻(xiàn)構(gòu)造,學(xué)會(huì)進(jìn)行Android

應(yīng)用程序開發(fā)。

2.理解Android顧客界面的基本概念,掌握TcxtVicw、EditTcxt,Button^ImagcButton^Checkbox>

RadioButton、Spinner、ListView等常用控件的使用。

3.理解Android系統(tǒng)的6種界面布局,掌握線性布局、框架布局、表格布局、相對(duì)布局、絕對(duì)布局、網(wǎng)格布

局等界面布局的使用。

二、實(shí)驗(yàn)內(nèi)容

開發(fā)如圖所示的Android應(yīng)用程序。⑹Expreimentl

功能及規(guī)定。

Helloworld!

1)在EditTcxu|i輸入數(shù)據(jù),當(dāng)點(diǎn)Null擊Input按鈕時(shí),

-------------------------------▲

把諭入的信息在TestView上顯示;CheckBoxlRadioButton

AA

2)當(dāng)選擇CheckBoxl時(shí),在CheckBox2RadioButtonTeslView上顯

示“CheckBoxl被選中";當(dāng)選擇CheckBox2Input時(shí),在

TcstV'icw上顯示“ChcckBox2被選中”;當(dāng)CheckBoxl和

CLose

LI_____i

CheckBox2均被選中時(shí),在TeslView上顯示**CheckBoxl

和CheckBox2被選中”;

3)當(dāng)選擇RadioButtonl時(shí),在TestView上顯示“RadioButtonl被選中”;當(dāng)選擇RadioButto12時(shí),在

TostViow上顯示“RadioButtonl被選中“;RadioButtonl和RadioButton2不能同步被選中;

4)點(diǎn)擊Close時(shí),關(guān)閉應(yīng)用程序,

三、試做環(huán)節(jié)和參照代碼

1.構(gòu)建實(shí)驗(yàn)環(huán)境

1)下載adt-bundle-windo\vs-x86-()917.(集成Android4.3版本);

2)安裝JDK;

3)注冊(cè)環(huán)境變量。

2.創(chuàng)立新的Android程序過程

1)打開Android工程向?qū)?File-*Ne\v—Project…|Android-*AndroidProject;

2)填寫工程名稱、選擇程序運(yùn)營(yíng)的Android系統(tǒng)版本、填寫應(yīng)用程序名稱

3)點(diǎn)擊“Finish”按鈕,工程向?qū)?huì)根據(jù)顧客所填寫的Android工程信息,自動(dòng)在后臺(tái)創(chuàng)立Android

工程所需要的基本文獻(xiàn)和目錄構(gòu)造。

3.編程實(shí)現(xiàn)實(shí)驗(yàn)內(nèi)容

【重要參照代碼】

1)界面布局

<?xmlversion="l.0"encoding=/,utf-8*?>

<LinearLayoutxmlns:android="”

android:orientation=,vertica1”

android:layoutwidth=z,fillparent*

android:layout_height=,/fill_parent/,>

<TextViewandroid:id="@+id/TextView01”

android:layout_width=/,wrap_content/,

android:layout_height=z,wrap_content,/

android:text="TextViewOl”>

</TextView>

<EditTextandi'oid:id="@+id/EditText01”

android:layout_width二〃fi1l_parent”

cindroid:layout_height="wrap_content”

android:text=*Nu11*>

</EditText>

<CheckBoxandroid:id="@+id/CheckBox01”

android:1ayout_width=/,wrap.content^

android:layoutheight="wrapcontent”

android:text=,zCheckBoxOl,z>

</CheckBox>

<CheckBoxandroid:id="@+id/CheckBox02〃

android:1ayoutwidth=,,wrapcontent”

android:layoutheight="wrap_content〃

android:text="CheckBox02”>

</CheckBox>

<RadioGroupandroid:id=/,@+id/RadioGroupO1

android:1ayout_width=/,wrap_contcnt”

android:layout_height=,/wrap_content”>

<RadioButtonandroid:id="@+id/RadioButtonOl”

android:1ayoutwidth=*wrap_content”

android:layout_height="wrap_content”

android:text="RadioButtonOl”>

</RadioButton>

<RadioButtonandroid:id="@+id/RadioButton02"

android:layoul_widlh="wrap_conlenl”

android:layout_height=^wrap_content〃

android:text=",RadioButton02,z>

</RadioButton>

</RadioGroup>

<Buttonandroid:id="0+id/Button01〃

android:layout_width=/,wrap_contcntz,

android:layoutheight="wrap_content”

android:text="Input”>

</Button>

<Buttonandroid:id="@+id/Button02”

android:layout_\vidth="wrap_content”

android:layout_height="wrap_content”

android:text="Close”>

</Button>

</LincarLayout>

2)獲取各控件ID

finalTextViewtextView=(TextView)findViewById(R.id.TextViewOl);

finalEditTexieditTcxt=(EditTexi)findViewByTd(R.id.EditText01);

finalCheckBoxchecKBoxl=(CheckBox)findViewJyld(R.id.CheckBoxOl);

finalCheckBoxchcckBox2=(CheckBox)findViewById(R.id.CheckBox02);

finalRadioButtonradioButtonl=(RadioButton)findViewById(R.id.RadioButtonOl):

finalRadioButtonradioButton2=(RadioButton)findViewByld(R.id.RadioButton02);

Buttonbutton1=(Button)findViewById(R.id.ButtonOl);

Buttonbutton2=(Button)findVicwByld(R.id.Button02);

3)監(jiān)聽事件

CheckBox監(jiān)聽:

CheckBox.OnClickListenercheckboxListener=newCheckBox.OnClickListener(){

?Override

publicvoidonClick(Viewv){

switch(v.getldO){

caseR.id.CheckBoxOl:

textView.setText("CheckBoxOl,

isChecked:,?+String.valueOf(checkBoxl.isCheckedO));

return;

caseR.id.CheckBox02:

textView.setText(,,CheckBox02,

isChecked:,Z+String.valueOf(checkBox2.isCheckedO));

return;

)

}};

RadioButton監(jiān)聽:

RadioButton.OnClickListenerrcidioButtonListener=new

RadioButton.OnClickListoner(){

0Override

publicvoidonClick(Viewv){

switch(v.getld()){

caseR.id.RadioButtonOl:

textView.setText(^RadioButtonlisselected");

return;

caseR.id.RadioButton02:

textView.setTextC,RadioButton2isselected");;

return;

)

}};

radioButtonl.setOnClickListener(radioButtonListener);

rcidioButton2.setOnClickListener(rcidioButtonListener);

Button監(jiān)聽:

Buttonl.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewview){

Stringmsg=editText.getTextO.toStringO;

textView.setText(msg);

)

});

Button2.setOnClickListener(newView.OnClickListener(){

pub1icvoidonClick(Viewvicw){

finishO;

)

});

四、實(shí)驗(yàn)規(guī)定

1.準(zhǔn)時(shí)到指定實(shí)驗(yàn)室進(jìn)行實(shí)驗(yàn);

2.學(xué)生應(yīng)獨(dú)立完畢Android程序開發(fā)平臺(tái)的搭建;

3.會(huì)創(chuàng)立新的Android應(yīng)用程序及編輯已創(chuàng)立的Android應(yīng)用程序;

4.完畢實(shí)驗(yàn)內(nèi)容規(guī)定的Android應(yīng)用程序

五、實(shí)驗(yàn)報(bào)告

實(shí)驗(yàn)報(bào)告應(yīng)涉及如下內(nèi)容:

1.實(shí)驗(yàn)?zāi)康?

2.實(shí)驗(yàn)內(nèi)容;

3.Android應(yīng)用程序創(chuàng)立過程(工程名字用本人的姓或班內(nèi)序號(hào)+Experimentl構(gòu)成);

4.給出重要的程序代碼,如界面布局文獻(xiàn)、邏輯代碼文獻(xiàn)等中的實(shí)現(xiàn)代碼。

5.在報(bào)告中給出程序運(yùn)營(yíng)界面;

6.應(yīng)有必要的總結(jié)和分析。

實(shí)驗(yàn)二:Android顧客界面(2)

一、實(shí)驗(yàn)?zāi)康?/p>

1.掌握菜單資源的使用,可以編程實(shí)現(xiàn)選項(xiàng)菜單、子菜單和快捷菜單。

2.理解操作欄和Fragment的基本概念和使用措施。

3.熟悉UVC模型中的控制器概念和界面事件,可以解決單擊事件、按鍵事件、觸摸事件等界面事件的解決。

二、實(shí)驗(yàn)內(nèi)容

開發(fā)如圖所示的Android應(yīng)用程序。

le?oWoMMenuResourceAclivity!HEO.R?ID:2131099649HED.?*10:1

打印打印打印

新建新建

郵件郵件郵件

ifil設(shè)置設(shè)置

訂閱訂閱訂閱

(1)(2)(3)

功能及規(guī)定。

1)圖(1)所示為程序的初始界面,給出使用在XML文獻(xiàn)中定義和邏輯代碼動(dòng)態(tài)生成兩種菜單定義措

施的實(shí)現(xiàn)代碼;

2)定義菜單的的點(diǎn)擊事件,該事件的解決過程是:若某個(gè)菜單子項(xiàng)被選中,在TextVie”控件顯示

該菜單子項(xiàng)的名稱和菜單ID;

3)圖(2)所示是使用XML文獻(xiàn)措施實(shí)現(xiàn)菜單定義時(shí),點(diǎn)擊“打印”子項(xiàng)時(shí)的顧客界面;

4)圖(3)所示是使用邏輯代碼動(dòng)態(tài)生成措施實(shí)現(xiàn)菜單定義時(shí),點(diǎn)擊“打印”子項(xiàng)時(shí)的顧客界面。

三、試做環(huán)節(jié)和參照代碼

【重要參照代碼】

1)XML實(shí)現(xiàn)措施

<?xmlversion="1.0"encoding="utf-8”?>

<menuxmlns:android=/,,/>

<itemandroid:id=*@+id/mainmenuO^

android:icon=*6drawab1e/picO”

android:title="打印"/>

<itemandroid:id=z,@+id/main_menu_1z,

android:icon=,,@drawab1c/pic1”

android:title="新建"/>

<itemandroid:id="@+id/mainmenu2”

android:icon="@drawable/pic2”

android:title="郵件"/>

<itemandroid:id="@+id/main_menu_3〃

android:icon="@drawab1e/pic3”

android:title="設(shè)立”/>

<itemandroid:id=z,@+id/main_menu_4z,

android:icon=〃@dra\vable/pic4,/

android:title—訂閱”/>

</menu>

2)獲取各控件ID

finalstaticintMENUOOMenu.FIRST;

finalstaticintMENL1_O1=Menu.FIRST+1:

finalstaticintMENU02Menu.FIRST+2;

finalstaticintMENU03Menu.FIRST+3;

finalstaticintMENU_04=Menu.FIRST+4;

publicbooleanonCreateOptionsMenu(Menumenu){

menu,add(0,MENU00,0,"打‘印").setIcon(R.drawatle.picO);

menu.add(0,MENU_01,1,〃新建〃).setIcon(R.drawatle.picl);

menu,add(0,MENU_02,2,〃郵件").setlcon(R.drawatle.pic2);

menu.add(0,MENU03,3,"設(shè)立").setIcon(R.drawatle.pic3);

menu,add(0,MENU04,4,〃訂閱〃).setIcon(R.drawatle.pic4);

returntrue;

}

3)邏輯代碼

publicbooleanonOptionsItemSe1ected(MenuItemitem){

TextViewlabel=(TextVicw)findViewByTd(R.id.label);

switch(item,getltemld()){

caseMENU_00:

label.setText("打印,菜單ID:"+item,getltemld());

returntrue;

caseMENU01:

label.setText(〃新建,菜單ID:〃+item.getltemldO);

returntrue;

caseMENU_02:

label.setText(“郵件,菜單ID:〃+item.getltemldO);

returntrue;

caseMENU_03:

label.setText("設(shè)立,菜單ID:"+item.getltemldO);

returntrue;

caseMENU_04:

label.setText("訂閱,菜單ID:"+item.getltemldO);

returntrue;

default:

returnfalse;

)

}

四、實(shí)驗(yàn)規(guī)定

1.準(zhǔn)時(shí)到指定實(shí)驗(yàn)室進(jìn)行實(shí)驗(yàn);

2.完畢實(shí)驗(yàn)內(nèi)容規(guī)定的Android應(yīng)用程序

五、實(shí)驗(yàn)報(bào)告

實(shí)驗(yàn)報(bào)告應(yīng)涉及如下內(nèi)容:

1.實(shí)驗(yàn)?zāi)康模?/p>

2.實(shí)驗(yàn)內(nèi)容;

3.Android應(yīng)用程序創(chuàng)立過程(工程名字用本人的姓或班內(nèi)序號(hào)+Expcrimcnt2構(gòu)成);

4.給出重要的程序代碼,如界面布局文獻(xiàn)、菜單定義文獻(xiàn)、邏輯代碼文獻(xiàn)等中的實(shí)現(xiàn)代碼。

5.在報(bào)告中給出程序運(yùn)營(yíng)界面;

6.應(yīng)有必要的總結(jié)和分析。

實(shí)驗(yàn)二:Intent及組件通信

一、實(shí)驗(yàn)?zāi)康?/p>

1.理解使用Intent進(jìn)行組件通信的原理。

2.掌握使用握tent啟動(dòng)Activity的措施。

3.掌握獲取Activity返回值的措施。

二、實(shí)驗(yàn)內(nèi)容

開發(fā)如圖所示的Android應(yīng)用程序。

(1)(2)(3)

功能及規(guī)定。

1)圖(1)為程序的初始界面(父Activity),其重要功能是提供1個(gè)TcxtVicw和兩個(gè)Button,點(diǎn)擊

“啟動(dòng)Activity1”則啟動(dòng)圖(2)所示的SubActivityl(子Activity),點(diǎn)擊“啟動(dòng)Activity?”則啟動(dòng)

圖(3)所示的SubActivity2(子Activity);

2)SubActivityl涉及1個(gè)TextView、1個(gè)EditText和兩個(gè)Button,TextView用于提示目前Activity是

子Activity,EditText用于輸入返回父Activity的信息,點(diǎn)擊“接受"Button則關(guān)閉SubActivityl并把輸

入信息返回,點(diǎn)擊"撤銷"Button則直接關(guān)閉SubActivityl;

3)當(dāng)有信息從了,Activity返回時(shí),父Activity接受返回信息并把它顯示在TextView控件上;

4)SubActivity2涉及1個(gè)TextView、"'Button,TextView用于提示目前Activity是子Activity,Button

則直接關(guān)閉SubActivity2o

三、試做環(huán)節(jié)和參照代碼

【重要參照代碼】

1)父Activity

<?xmlversion.1.0"encoding=

<LinearLayoutxmlns:android:”"

android:orientation=zvertica1”

android:layout_width="fill_parcnt”

android:layout_heighl=/,fi1l_parent/,

>

<TextView

android:id="@+id/tcxtSho'v"

android:layout_width=,,fi1l_parent,/

android:layoutheight="wrapcontent”

android:text=**

/>

<Button

android:id=*@+id/btnl*

android:1ayoutwidth="wrap_content”

android:1ayout_height=,,wrap_content

android:text="啟動(dòng)Activity/

/>

<Button

android:id="@+id/btn2”

android:layout_width=,/wrapcontent”

android:layout_height=,,wrap_content/,

android:text="啟動(dòng)Activity2”

/>

</LinearLayout>

2)SubActivityl

<?xmlvcrsion=〃l.0"encoding=,,utf-8,,?>

<LinearLayoutxm1ns:android=z,z,

android:orientalion-vertical”

android:layoutwidth=,,fi1l_parent,,

android:layout_height=z,fi11.parent”

>

<TextView

android:id=〃@+id/iextShow”

android:layout_width="zfi1l_parent,,

android:layoutheight="wrapcontent

android:text=/,SubActivity1”

/>

<EditText

android:id=,,@+id/edit,/

android:layoutwidth=,/fi1l_parent,/

android:layoutheight="wrapcontent'

android:text=””

/>

<Button

cindroid:id="@+id/btn_ok〃

android:layoutwidth=〃100dip〃

android:1ayout_height=,,wrap_content'

android:lext=〃接受“

/>

<Button

android:id=,,@+id/btn_cancel?,

android:layout_width=〃100dip”

android:layoutheight=,,wrap_content,

android:text="撤銷"

/>

/>

</LinearLciyout>

3)SubActivity2

<?xmlversion=*l.0,zencoding=/,utf-8,,?>

<LinearLayoutxm1ns:android=””

android:orientation二〃vertical〃

android:layoutwidth=,,fi1l_parcnt

android:layout_height=,,fi1l_parent,,

>

<TextView

android:id="@+id/textShow”

android:1ayoutwidth二〃filIparent〃

android:1ayout_height=/,wrap_contcnt,/

android:text="SubActivity2”

/>

<Button

android:id=,,@+id/btn_return,/

android:layoutwidth="lOOdip”

cindroid:layout_height=,,wrcip_content”

android:text="關(guān)閉”

/>

</LinearLayout>

4)子Activity注冊(cè)

<activityandroid:label=,,@string/appname,,

android:namc=".SubActivity1〃>

</activity>

<activityandroid:1cibel=,,@string/appname^

android:name=,/.SubActivity2〃>

</activity>

4)SubActivityl定義

publicclassSubActivitylextendsActivity{

Override

publicvoidonCreate(BundlesavedlnstanceState){

super.onCreate(savedlnstanceState);

setContentView(R.layout,subactivityl);

finalEditTexteditText=(EditText)findViewDyld(R.id.edit);

ButtonbtnOK="Button)findViewById(R.id.btnok);

ButtonbtnCance'=(Button)findViewById(R.id.btn_cancel);

btnOK.setOnClickListener(newOnClickListener(){

publicvoidonClick(Viewview){

StringuriString=editText.getText().toStringO;

Uridata=Uri.parse(uriString);

Intentresult=newIntent(null,data);

setResult(RESULT_OK,result);

finish();

)

t);

btnCanccl.setOnClickListcncr(newOnClickListcncrO{

publicvoidonClick(Viewview){

setResult(RESULT_CANCELED,null);

finishO;

}

});

}

)

5)SubActivity2定義

publicclassSubActivity2extendsActivity{

./*.Calle,whe.th.activit.i.firs,created.*/

?Override

publicvoidonCreatc(Bundlesaved!nstanceState){

super.onCreate(savedlnstanceState);

sctContentView(R.layout.subactivity2);

ButtonbtnReturn=(Button)findViewByld(R.id.btnreturn);

btnRcturn.sctOnClickListcncr(newOnClickListcncrO{

publicvoidonClick(Viewview){

setResull(RESULT_CANCELED,null);

finish();

)

});

}

)

6)重要邏輯代碼

publicclassActivityConmunicationActivityextendsActivity{

privatestaticfinalintSL-BACTIVITYl=1;

privatestaticfinalintSUBACTTVTTY2=2;

TextViewtextView;

../*.Calle,whe.th.activit.i.firs,created.*/

@Override

publicvoidonCreate(BundlesavedinstanceState){

super.onCreate(savedlnstanceState);

setContentView(R.layout,main);

textView=(TextView)findViewByld(R.id.textShow);

finalButtonbtnl=(Button)findViewByld(R.id.btnl);

finalButtonbtn2=(Button)findViewByld(R.id.btn2);

btnl.setOnClickListener(newOnClickListencr(){

pub1icvoidonC1ick(Viewview){

Intentintent=newIntent(ActivityCommunicationActivity.this,

SubActivityl.class);

startActivityForResult(intent,SUBACTIVITY1);

}

});

btn2.sctOnClickListener(newOnClickListencr(){

publicvoidonClick(Viewview){

Intentintent=newIntent(ActivityCommunicationActivity.this,

SubActivity2.class);

startActivityForResult(intent,SUBACTTVTTY2);

}

});

)

@Override

protectedvoidonActivityResult(intrequestCcde,intresultCode,Intentdata){

super.onActivityResult(requestCode,resultCode,data);

switch(requestCode){

caseSUBACTIVITY1:

if(resultCode==RESULT_OK){

UriuriData=data.getDataO;

textView.setText(uriData.toStringO);

)

break;

caseSUBACTIVITY2:

break;

)

1

四、實(shí)驗(yàn)規(guī)定

1.準(zhǔn)時(shí)到指定實(shí)驗(yàn)室進(jìn)行實(shí)驗(yàn);

2.完畢實(shí)驗(yàn)內(nèi)容規(guī)定的Android應(yīng)用程序

五、實(shí)驗(yàn)報(bào)告

實(shí)驗(yàn)報(bào)告應(yīng)涉及如下內(nèi)容:

1.實(shí)驗(yàn)?zāi)康模?/p>

2.實(shí)驗(yàn)內(nèi)容;

3.Android應(yīng)用程序創(chuàng)立過程(工程名字用本人的姓或班內(nèi)序號(hào)+ExDerimenl3構(gòu)成):

4.給出重要的程序代碼,如界面布局文獻(xiàn)、菜單定義文獻(xiàn)、邏輯代碼文獻(xiàn)等中的實(shí)現(xiàn)代碼。

5.在報(bào)告中給出程序運(yùn)營(yíng)界面;

6.應(yīng)有必要的總結(jié)和分析。

實(shí)驗(yàn)四:Service及后臺(tái)服務(wù)

一、實(shí)驗(yàn)?zāi)康?/p>

1.理解Service的原理和用途,掌握本地服務(wù)的管理措施。

2.掌握服務(wù)的隱式啟動(dòng)和顯式啟動(dòng)措施。。

3.理解線程的啟動(dòng)、掛起和停止措施,理解跨線程的界面更新措施。

二、實(shí)驗(yàn)內(nèi)容

開發(fā)如圖所示的Android應(yīng)用程序。

(1)(2)(3)

功能及規(guī)定。

1)圖(1)為程序的初始界面,設(shè)計(jì)4個(gè)TextView和3個(gè)Button

2)點(diǎn)擊“服務(wù)綁定"Button,則將已定義好的“MathService”服務(wù)綁定到本進(jìn)程(aMathServiceM

服務(wù)的功能是提供“+、-、*、/"四個(gè)運(yùn)算);

3)點(diǎn)擊“取消綁定"Button,則將“MathService”服務(wù)與本進(jìn)程的綁定取消;

4)點(diǎn)擊“數(shù)學(xué)運(yùn)算”Button,則一方面生成兩個(gè)0T00的隨機(jī)數(shù),然后分別計(jì)算這兩個(gè)數(shù)的利、差、積和

商;并分別顯示在4個(gè)TextView上;若點(diǎn)擊“數(shù)學(xué)運(yùn)算”Button時(shí),uMathServicew服務(wù)未被綁定或已經(jīng)

解除了綁定,則顯示如圖(3)所示信息。

三、試做環(huán)節(jié)和參照代碼

【重要參照代碼】

1)界面設(shè)計(jì)

<?xnilversion="1.0"encoding="utf-8"?>

<LincarLayoutxinlns:android=""

android:orienlation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

<TcxtVicwandroid;id="@+id/liibcl1"

android:layout_width="fill_parcnt"

android:layout_height="wrap_content"

android:text="addResult">

</TextView>

<TcxtVicvvandroid:id="@+id/labcl2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=',subResult,,>

</TextView>

<Tex(Viewandroid:id="@+id/label3,'

android:layout-width="fill_parent"

android:kiyout_hcight="wrap_contcnt”

android:lext="mulResult">

</TextView>

<TextVievvandroid:id="@+id/label4"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="divResult">

</TextView>

<Buttonandroid:id="@+id/bind"

android:layoul_width="wrap_content"

android:layout_height="wrap_content"

android:tnxt="服務(wù)綁定”>

</Button>

<Buttonandroid:id='@+id/unbind"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:iex[="取消綁定")

</Button>

<Buttonandroid:id="@+id/compute"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="數(shù)學(xué)運(yùn)算”>

</Button>

</LinearLayout>

2)服務(wù)定義

publicclassMathScrviceextendsService{

privatefinalIBindermBinder=newLocalBinder();

publicclassLocalBinderextendsBinder{

MathServicegetSer\ice(){

returnMathService.this;

@Ovcrridc

publicIBinderonBind(Imentintent){

Toast.makeText(thi$,“本地綁定:MathService",

Toast.LENGTH_SHORT).show();

returnmBinder;

I

?Override

publicbooleanonllnbindilntcntintcnt){

Toast.makeTexl(this,”取消本地綁定:MathService",

Toast.LENGTH_SHORT).show();

returnfalse;

)

publiclongAdd(longa,longb){

returna+b;

1

publiclongSub(longa,longb){

returna-b;

1

publiclongMul(longa,longb){

returna*b;

)

publicdoubleDiv(longa,longb){

return(double)a/(double)b;

)

I

3)服務(wù)注冊(cè)

<serviceandroid:name=".MathService'7>

4)重要邏輯代碼

publicclassMainActivityextendsActivity{

privateMathServicemathService;

privatebooleanisBound=false;

TextViewaddLable,subLable,niulLable,divLable;

@Override

publicvoidonCreate(Bun(JlesavedlnstanceState){

supcr.onCrcatc(savcdlnstanccStcUc);

sctContcntVicw(R.layout.activity_main);

addLable=(TextView)findViewByld(R.id.labelI);

subLable=(TextView)findViewById(R.id.label2);

mulLablc=(TextView)findViewByld(R.id.labeB);

divLable=(TextView)findViewById(R.id.label4);

ButtonbindButton=(Button)findViewByld(R.id.bind);

ButtonunbindButton=(Button)findViewByld(R.id.unbind);

ButtoncomputButton=(Button)findViewByld(R.pute);

bindButton.setOnClickListener(newView.OnClickListener()(

@Ovcrridc

publicvoidonClick(Viewv){

if(!isBound){

finalIntentserviceintent=newIntent(MainActivity.this,MathService.class);

bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE);

isBound=true;

)

)

});

unbindButton.se(OnClickListener

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論