付費下載
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】AndroidService詳解(三)AIDL使用解析
aidl是AndroidInterfacedefinitionlanguage的縮寫,它是一種android內(nèi)部進(jìn)程通信接口的描述語言,通過它我們可以定義進(jìn)程間的通信接口。
通過aidl我們可以完成從服務(wù)端到客戶端的數(shù)據(jù)通信
在aidl中我們可以聲明任意多個方法,除了內(nèi)建類型(intboolean等)都需要導(dǎo)入,規(guī)則如下:
1、Java原始類型不需要導(dǎo)入。
2、String、Lsit、Map和CharSequence不需要導(dǎo)入。
創(chuàng)建aidl文件,New->file->文件名.aidl,添加如下代碼:package
com.example.new1;
interface
INewService
{
void
setAge(int
age);
int
getAge();
void
setName(String
name);
String
getName();
}點擊保存,刷新工程,會在gen下自動產(chǎn)生java代碼。(產(chǎn)生的代碼有時候沒有縮進(jìn),可以右鍵->Source->Format進(jìn)行設(shè)置)
在生成的代碼中又一個Stud類,他繼承于IBinder。可以把它作為Service的onBind的返回值,一旦這個Service被其他程序的服務(wù)綁定,就將這個IBinder類實例發(fā)送出去,同樣這個實例里重寫的方法數(shù)據(jù)也跟著一起發(fā)送出去。新建一個Servers,命名為NewService.java里面聲明一個Stub類,完成上面定義的四個函數(shù),代碼如下:package
com.example.new1;
import
com.example.new1.INewService.Stub;
import
android.app.Service;
import
android.content.Intent;
import
android.os.Bundle;
import
android.os.IBinder;
import
android.os.RemoteException;
import
android.util.Log;
import
android.widget.Toast;
public
class
NewService
extends
Service
{
private
String
name="";
private
int
age=18;
@Override
public
IBinder
onBind(Intent
arg0)
{
//
TODO
Auto-generated
method
stub
Toast.makeText(NewService.this,
"onBind",
Toast.LENGTH_LONG).show();
Log.i("SERVICE","onbind");
return
mbinder;
//返回接口
}
public
void
onCreate()
{
super.onCreate();
Log.i("SERVICE","oncreat");
}
public
void
onStart(Intent
intent,int
startId)
{
Log.i("SERVICE","onstart");
}
public
void
onDestroy()
{
Log.i("SERVICE","ondestory");
}
private
INewService.Stub
mbinder
=
new
Stub()
{
@Override
//實現(xiàn)接口定義的函數(shù)
public
void
setAge(int
age)
throws
RemoteException
{
//
TODO
Auto-generated
method
stub
NewService.this.age
=
age;
}
@Override
public
int
getAge()
throws
RemoteException
{
//
TODO
Auto-generated
method
stub
return
NewService.this.age;
}
@Override
public
void
setName(String
name)
throws
RemoteException
{
//
TODO
Auto-generated
method
stub
NewS=name;
}
@Override
public
String
getName()
throws
RemoteException
{
//
TODO
Auto-generated
method
stub
return
NewS;
}
};
}
到目前為止,已經(jīng)實現(xiàn)了接口中的全部函數(shù),下面,將實現(xiàn)客戶端的調(diào)用:新建一個Activity.java,代碼如下:package
com.example.new1;
import
android.app.Activity;
import
android.app.ActionBar;
import
android.app.Fragment;
import
android.content.ComponentName;
import
android.content.Intent;
import
android.content.ServiceConnection;
import
android.os.Bundle;
import
android.os.IBinder;
import
android.os.RemoteException;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.view.ViewGroup;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import
android.os.Build;
public
class
MainActivity
extends
Activity
{
private
TextView
textview;
private
INewService
inewservice;//聲明接口
private
ServiceConnection
conn=new
ServiceConnection()
{
@Override
public
void
onServiceConnected(ComponentName
arg0,
IBinder
arg1)
{
//
TODO
Auto-generated
method
stub
inewservice
=
INewService.Stub.asInterface(arg1);//獲得接口
try
{
inewservice.setName("我是Activity");
//調(diào)用函數(shù)
inewservice.setAge(25);
textview.setText(inewservice.getName()+inewservice.getAge());
}
catch
(RemoteException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
Log.i("SERVICE","success"
);
}
@Override
public
void
onServiceDisconnected(ComponentName
arg0)
{
//
TODO
Auto-generated
method
stub
Log.i("SERVICE","errer");
}
};
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button
button1=(Button)this.findViewById(R.id.btn1);
textview=(TextView)this.findViewById(R.id.mytext1);
button1.setOnClickListener(new
OnClickListener()
{
@Override
public
void
onClick(View
arg0)
{
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 南京交安考試試題及答案
- 系統(tǒng)工程師考試題及答案
- 呼和浩特安全員b證考試題及答案
- 農(nóng)村信用社筆試試題及答案
- 黨紀(jì)知識競賽題庫及答案
- 質(zhì)檢員專業(yè)管理實務(wù)復(fù)習(xí)模擬試題及答案
- 重慶中職計算機(jī)題庫及答案
- 鐵路職業(yè)技能鑒定試題預(yù)測試卷附答案詳解
- 醫(yī)技三基三嚴(yán)模考試題+答案
- 保育員高級理論知識試卷及答案2
- 中華人民共和國職業(yè)分類大典是(專業(yè)職業(yè)分類明細(xì))
- 2025年中考英語復(fù)習(xí)必背1600課標(biāo)詞匯(30天記背)
- 資產(chǎn)管理部2025年工作總結(jié)與2025年工作計劃
- 科技成果轉(zhuǎn)化技術(shù)平臺
- 下腔靜脈濾器置入術(shù)的護(hù)理查房
- 基建人員考核管理辦法
- 2025體育與健康課程標(biāo)準(zhǔn)深度解讀與教學(xué)實踐
- 礦山救援器材管理制度
- 2025西南民族大學(xué)輔導(dǎo)員考試試題及答案
- T/CSPSTC 17-2018企業(yè)安全生產(chǎn)雙重預(yù)防機(jī)制建設(shè)規(guī)范
- 2025年《三級物業(yè)管理師》考試復(fù)習(xí)題(含答案)
評論
0/150
提交評論