安卓Service組件使用系列6:使用AIDL完成兩個進程間的通信

Android Interface Definition Language (AIDL)  安卓接口定義語言,定義這樣的接口,必須以.aidl作爲後綴名。使用這樣的接口定義,用於兩個進程間的通信(兩個apk之間需要一個通信功能時用到)。這時需要定義.aidl文件。下面我們來看一下它的使用方法。

整體思路:創建一個安卓工程android_service_aidl_server,作爲服務器端,定義一個MyService類,繼承Service,定義一個binder對象,重寫getData方法,在這個方法中根據參數arg的不同返回不同的結果。定義一個名爲DataService.aidl的文件作爲安卓接口聲明接口中的getData方法。創建另一個安卓工程android_service_aidl_client,作爲客戶端端,定義一個與第一個工程相同的名爲DataService.aidl的文件,在xml'文件中放置兩個Button控件,定義一個ServiceConnection對象,在這個對象的onServiceConnected方法中實例化DataService對象,設置兩個Button控件的點擊事件,在第一個點擊事件中綁定service,在第二個點擊事件中通過DataService對象獲取服務器端對應參數的數據。

android_service_aidl_server->MyService.java文件:

package com.example.android_service_aidl_server;

import com.example.service.DataService;

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

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return binder;
	}
	
//	定義提供給客戶端(Client端)調用的方法
	Binder binder=new DataService.Stub() {
		
		@Override
		public double getData(String arg) throws RemoteException {
			// TODO Auto-generated method stub
			if(arg.equals("a")){
				return 1;
			}else if(arg.equals("b")){
				return 2;
			}
			
			return 0;
		}
	};

}
android_service_aidl_server->DataService.aidl文件:

package com.example.service;

interface DataService{
   double getData(String arg);
}
android_service_aidl_client->activity_main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:text="綁定Service" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="進程間的通信" />

</RelativeLayout>
android_service_aidl_client->MainActivity.java文件:

package com.example.android_service_aidl_client;
//進程間的通信:兩個apk之間需要一個通信的功能時用到
import java.sql.Connection;

import com.example.service.DataService;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button,button2;
	private DataService dataService;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button=(Button)findViewById(R.id.button1);
		button2=(Button)findViewById(R.id.button2);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(DataService.class.getName());
				bindService(intent, connection, BIND_AUTO_CREATE);
			}
		});
		
		button2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
//					從另外一個工程中獲取一個值
					double result=dataService.getData("a");
					System.out.println("-->"+result);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		});
		
	}

	private ServiceConnection connection=new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			dataService=DataService.Stub.asInterface(service);
			
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
android_service_aidl_client->DataService.aidl文件:

package com.example.service;

interface DataService{
   double getData(String arg);
}






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