【移動應用開發(fā)技術(shù)】AIDL簡單示例_第1頁
【移動應用開發(fā)技術(shù)】AIDL簡單示例_第2頁
【移動應用開發(fā)技術(shù)】AIDL簡單示例_第3頁
【移動應用開發(fā)技術(shù)】AIDL簡單示例_第4頁
免費預覽已結(jié)束,剩余1頁可下載查看

下載本文檔

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

文檔簡介

【移動應用開發(fā)技術(shù)】AIDL簡單示例

1).AIDL簡介:AIDL(AndroidInterfaceDefinitionLanguage),即安卓接口定義語言。AIDL主要是用于進程對遠程Service的通信,也就是一個進程采用AIDL可以啟動另一個進程的Service,并從該Service中獲取數(shù)據(jù)(通信)。2).具體做法:1.首先創(chuàng)建一個AIDL接口代碼://com.example.aidl.AidlGetServiceData.aidl

package

com.example.aidl;

interface

AidlGetServiceData

{

int

getAge();

String

getName();

}/*

注:AIDL定義接口的源代碼必須以.aidl結(jié)尾。

AIDL接口中用到的數(shù)據(jù)類型,除了基本類型,String,List,Map,CharSequence之外,其他類型均全部需要導包。

定義好上面的AIDL接口后,ADT工具會自動在gen/com/example/aidl中生成一個AidlGetServiceData.java接口,在該接口里面包含一個Stub內(nèi)部類,該類實現(xiàn)了IBinder和AidlGetServiceData兩個接口,這個Stub類將會作為遠程Service的回調(diào)類因為他實現(xiàn)了IBinder的接口,因此可以作為Service的onBind()方法的返回值。public

static

abstract

class

Stub

extends

android.os.Binder

implements

com.yn.aidl.AidlGetServiceData

*/

2.定義好AIDL接口后,就可以著手遠程Service的編寫了。

//src/com.example.aidl_service.AIDLService.java

package

com.example.aidl_service;

import

com.example.aidl.AidlGetServiceData;

import

android.app.Service;

import

android.content.Intent;

import

android.os.IBinder;

import

android.os.RemoteException;

public

class

AIDLService

extends

Service

{

private

int

age;

private

String

name;

@Override

public

void

onCreate()

{

super.onCreate();

this.age

=

10;

=

"get

data

from

Service

using

aidl";

}

//由于Stub是抽象類,故在這創(chuàng)建一個子類,獲取Service的數(shù)據(jù),作為onBind()的返回值,攜帶Service的數(shù)據(jù)。

public

class

AidlGetServiceDataBinder

extends

AidlGetServiceData.Stub

{

@Override

public

int

getAge()

throws

RemoteException

{

return

age;

}

@Override

public

String

getName()

throws

RemoteException

{

return

name;

}

}

@Override

public

IBinder

onBind(Intent

intent)

{

//

返回AidlGetServiceDataBinder的實例

return

new

AidlGetServiceDataBinder();

}

}3.Service類開發(fā)完成后,還必須在AndroidMainfest.xml中進行聲明:<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme"

>

<service

android:name="com.example.aidl_service.AIDLService"

>

<intent-filter>

<action

android:name="com.example.aidl.action.AIDL_TEST"

/>

</intent-filter>

</service>

</application>經(jīng)過以上步驟,遠程Service便已經(jīng)完成了。接下來,可以創(chuàng)建另一個進程來通過AIDL獲取到遠程Service中的數(shù)據(jù)。創(chuàng)建一個android應用程序,在Activity中添加兩個Button和兩個TextView,分別用來顯示從遠程Service中讀取的數(shù)據(jù)。具體做法:1.創(chuàng)建一個應用程序后,首先將上面定義好的AIDL接口拷貝到工程目錄中,同理,ADT工具會自動在gen/com/example/aidl中生成一個AidlGetServiceData.java接口。2.實例化一個ServiceConnection對象,該對象的onServiceConnected((ComponentNamename,IBinderservice))方法中的service參數(shù)就是遠程Service的onBind()方法中的返回值對象的代理,因此,要獲取onBind()返回值對象,還需進行如下處理:private

ServiceConnection

conn

=

new

ServiceConnection()

{

@Override

public

void

onServiceConnected(ComponentName

name,

IBinder

service)

{

aidlService

=

AidlGetServiceData.Stub.asInterface(service);

}

@Override

public

void

onServiceDisconnected(ComponentName

name)

{

aidlService

=

null;

}

};3.在Activity的onCreate()方法中,啟動遠程Service。Intentintent=newIntent("com.example.aidl.action.AIDL_TEST");bindService(intent,conn,Service.BIND_AUTO_CREATE);4.經(jīng)過以上步驟,便得到了遠程Service中的onBind()返回值對象,則可由該對象提供的接口獲取Service中的數(shù)據(jù)。public

void

onGetAge(View

view)

{

try

{

int

age

=

aidlService.getAge();

tvAge.setText(age+"");

}

catch

(RemoteException

e)

{

e.printStackTrace();

}

}

public

void

onGetName(View

view)

{

try

{

String

name

=

aidlService.getName();

tvName.setText(name

溫馨提示

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

評論

0/150

提交評論