簡單明瞭的AIDL使用

AIDL(Android Interface Definition Language,Android接口定義語言)是IPC(Inter Process communication,進程間通信),由字面可以看出可以用於進程之間通信,同樣和可以用於activity和service。對於一個像我一樣的新手來說,看了很多網上的介紹第一感覺高大上,第二感覺需要長時間理解,下面根據我自己的理解描述AIDL的使用,以實現最簡單的兩數加和爲例,建立工程,xml佈局等步驟忽略。


一.建立一個AIDL文件(即一個Interface)

interface Add{
    int add(in int num1, in int num2);
}

一旦文件被保存,Android的AIDL工具會在gen/com/android/hellosumaidl這個文件夾裏自動生成對應的Add.java這個文件。因爲是自動生成的,所以無需改動。這個文件裏就包含了Stub,我們接下來要爲我們的遠程服務實現這個Stub。

二.在已有的Service中重寫OnBind()方法(沒有則建立)

public class AddService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }                                                                                                                                                                                                                                                                                                                                                                                                  
    @Override
    public IBinder onBind(Intent intent) {
        return new Add.Stub() {
            @Override
            public int add(int num1, int num2) {
                return num1 + num2;
            }
        };
    }
                                                                                                                                                                                                                                                                                                                                                                                                        
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
這樣做的目的就是爲下一步的ServiceConnection做好準備。


三.建立連接

有activity了,也有Service了,怎樣才能實現兩者的交互呢,那肯定要建立一個橋實現其連接的作用。
此處使用在activity中建立一個內部類並實現ServiceConnection接口。
實現ServiceConnection接口之後必須要重寫2個未實現的方法,爲
public void onServiceConnected(ComponentName name, IBinder boundService)
public void onServiceDisconnected(ComponentName name)

即連接成功之後和斷開連接時的回調,一般在連接成功後對接口的實現類進行實例化,即
service = Add.Stub.asInterface((IBinder)boundService);

四.使用

準備工作完成之後就可以進行使用了,即對連接對象進行實例化之後與Service綁定。

<span style="font-weight: normal;"><span style="font-size:14px;">public class AIDLActivity extends Activity {
    private Add service;
    private AddServiceConnection connection;
    private Button but=null;                                                                                                            private TextView res = null;   
    private EditText num1 = null;
    private EditText num2 = null;                                               
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                                                                                                                                 
        initService();
                                                                                                                                 
        but = (Button)findViewById(R.id.but);
        res = (TextView)findViewById(R.id.res);
        num1 = (EditText)findViewById(R.id.num1);
        num2 = (EditText)findViewById(R.id.num2);                                                                                                                         
        but.setOnClickListener(new OnClickListener() {
                                                                               
            @Override
            public void onClick(View v) {
                int n1, n2, r;
                n1 = Integer.parseInt(num1.getText().toString());
                n2 = Integer.parseInt(num2.getText().toString());
                                                                                                                                                    r = service.add(n1, n2);                
                                                                            
                res.setText(Integer.valueOf(r).toString());
            }
        });
    }
                                                                                                                             
    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeService();
    }
                                                                                                                             
   
    class AddServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName name, IBinder boundService) {
            service = Add.Stub.asInterface((IBinder)boundService);
            //其他功能
        }
                                                                                                                                 
        public void onServiceDisconnected(ComponentName name) {
            service = null;
            //其他功能
        }
    }
                                                                                                                             
   
    private void initService() {//初始化
        connection = new AddServiceConnection();
        Intent i = new Intent();
        i.setClassName("com.android.aidl",
com.android.aidl.AddService.class.getName());//包名和服務類名
        bindService(i, connection, Context.BIND_AUTO_CREATE);
    }
                                                                                                                             
   
    private void closeService() {//關閉服務
        unbindService(connection);
        connection = null;
    }
}</span></span>

此時,就可以實現activity和service之間的通信了。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章