android 不同進程間的調用 AIDL 實現通訊服務

android 不同進程間的調用  AIDL 實現通訊服務

      最近對aidl android 不同進程間的調用,不同運用間的調用做了一些嘗試:
步驟如下:
1:首先在要被調用的程序裏寫好服務service


具體代碼如下:
package com.jiaruihua.service.demo;

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

public class AlipayService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		
		System.out.println("遠程服務已綁定");
		<span style="color:#ff0000;">//注意此處要返回我們的IBinder</span>
		return new MyBinder();
	}
	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("遠程服務已解除");
		return super.onUnbind(intent);
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		System.out.println("遠程服務已創建");
		super.onCreate();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("遠程服務已銷燬");
		super.onDestroy();
	}
	/*
*<span style="color:#ff0000;">此處是遠程所要調用的方法,功能處理在此處</span>
	 * 支付操作
	 */
	public void payMoney(){
		
		System.out.println("支付操作");
		
	}
	/*
*此處需要一個Iservice.Stub,這是我們定義的Iservice.aidl,所實現的,見第二步
	 * Iservice.Stub 已經實現Binder 
	 */
	public class MyBinder extends <span style="color:#ff0000;">Iservice.Stub</span>{

		@Override
		public void callMethodService() {
			payMoney();
			
		}
		
	}

}

2:創建上一步中需要一個Iservice.aidl文件



代碼如下:

package com.jiaruihua.service.demo;
//aidl 接口定義不需要訪問修飾符 默認公共

interface Iservice {
void callMethodService();
}

eclipse中的gen目錄中會自動創建 Iservice.java文件

3:在mainfest.xml文件中註冊Iservice

4:在另外一個需要調用的程序中創建一個包,注意此處的包名要和 上面mainfest.xml中service的指定包名一致;
讓後將上面第二步創建的Iservice.aidl文件拷貝過來



5:調用遠程服務

package com.example.mygameapp;

import com.jiaruihua.service.demo.Iservice;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

	private Intent intent;

	private Iservice iservice;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		intent = new Intent();
		<span style="color:#ff0000;">// 此處的action 必須和你在調用的程序裏設置的action 一至</span>
		intent.setAction("com.jiaruihua.ServicePay");

		// 綁定服務
		findViewById(R.id.banding).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				<span style="color:#ff0000;">// 綁定服務 開啓服務</span>
				bindService(intent, new MyConn(), BIND_AUTO_CREATE);
			}
		});
		<span style="color:#ff0000;">// 調用遠程方法</span>
		findViewById(R.id.callmethod).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					iservice.callMethodService();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	private class MyConn implements ServiceConnection {
		// 成功綁定
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			<span style="color:#ff0000;">// 將sercicse 轉換成Iservice接口類型
			iservice = Iservice.Stub.asInterface(service);</span>
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}

	}

}




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