2012年2月18日 星期六

Android - AIDL

在Android中,每個應用程式都可以有自己的進程,在相同的進程中彼此可以共用記憶體資訊,但在不同的進程中,Java中不允許跨進程記憶體共用。

在Linux中是以處理程序為單元來進行資料的配置與管理,但是基於保護目的,一個處理程序不能直接存取另一個處理程序資源。

解決方法 :透過 IPC(Inter-Prcess Communication) 來溝通。

為了完成處理程序之間的通訊,Binder採用了AIDL來描述處理程序間的介面,讓Android得以實踐跨界傳值的目的。(跨界存取)


AIDL(Androoid Interface Definition Language)是一種介面描述語言,編譯器可以通過 .aidl 檔生成一段程式代碼,通過預先定義的介面達到兩個進程內部通訊的目的。


實作方法
IAddService.aidl

package com.aidl;


interface IAddService
{
int add(in int x, in int y);
}


AddService.java

package com.service;


import com.aidl.IAddService;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;


public class AddService extends Service
{
@Override
public IBinder onBind(Intent arg0) 
{
IAddService.Stub stub = new IAddService.Stub()
{
@Override
public int add(int x, int y) throws RemoteException
{
return x+y;
}
};

return stub;
}
}


AddServiceConnection.java
package com.service;

import com.aidl.IAddService;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;

public class AddServiceConnection implements ServiceConnection
{
private IAddService service; 
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
this.service = IAddService.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name)
{
this.service =  null;
}
public IAddService getService()
{
return this.service;
}
}

AIDL_Add.java
package com.test;

import com.aidl.IAddService;
import com.service.AddServiceConnection;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AIDL_Add extends Activity
{
private AddServiceConnection connection;
    private EditText editText01, editText02;
    private TextView textView01;
    private Button button01;
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initService();
        
        editText01 = (EditText)findViewById(R.id.editText1);
        editText02 = (EditText)findViewById(R.id.editText2);
        
        textView01 = (TextView)findViewById(R.id.textView2);
        
        button01 = (Button)findViewById(R.id.button1);
        
        button01.setOnClickListener(new Button01OnClickListener());
    }
    
    private void initService()
    {
     connection = new AddServiceConnection();
     Intent intent = new Intent("com.service.REMOTE_SERVICE");
     bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }
    
    class Button01OnClickListener implements OnClickListener
    {
@Override
public void onClick(View view) 
{
int xValue = Integer.parseInt(editText01.getText().toString());
int yValue = Integer.parseInt(editText02.getText().toString());
IAddService service = connection.getService();
try
{
if(service==null)
{
Toast.makeText(AIDL_Add.this, "Service is null", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(AIDL_Add.this, String.valueOf(service.add(xValue, yValue)), Toast.LENGTH_SHORT).show();
textView01.setText(String.valueOf(service.add(xValue, yValue)));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
    }
}

執行結果如下









沒有留言:

張貼留言