AIDL簡單示例

1).AIDL簡介:AIDL(Android Interface Definition Language),即安卓接口定義語言。

成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、遂平網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為遂平等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

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之外,其他類型均全部需要導(dǎo)包。

   

定義好上面的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;
this.name = "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應(yīng)用程序,在Activity中添加兩個Button和兩個TextView,分別用來顯示從遠程Service中讀取的數(shù)據(jù)。

具體做法:

1.創(chuàng)建一個應(yīng)用程序后,首先將上面定義好的AIDL接口拷貝到工程目錄中,同理,ADT工具會自動在gen/com/example/aidl中生成一個AidlGetServiceData.java接口。

2.實例化一個ServiceConnection對象,該對象的onServiceConnected((ComponentName name, IBinder service))方法中的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。

Intent intent = new Intent("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);
} catch (RemoteException e) {
e.printStackTrace();
}
}

5.最后,退出程序前,記得解除與Service的綁定。

@Override
protected void onDestroy() {
super.onDestroy();
this.unbindService(conn);
}

分享文章:AIDL簡單示例
文章鏈接:http://muchs.cn/article14/gpjdge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、云服務(wù)器、手機網(wǎng)站建設(shè)、搜索引擎優(yōu)化、面包屑導(dǎo)航、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站