码迷,mamicode.com
首页 > 其他好文 > 详细

AIDL调用指南

时间:2016-03-26 18:41:17      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

近期有需求要实现两个apk之间的通信,想到用AIDL来实现,现写一个demo学习下AIDL怎样使用。


这里我要实现一个apk(client端)调用还有一个apk(server端)的方法.

先实现server端。代码结构例如以下

技术分享

技术分享

AIDL文件内容例如以下:

package com.example.testaidl;
interface MyInterface {
    void testMethod();
}

MainActivity.java

package com.example.testaidlserver;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startService(new Intent(this, MyService.class));
	}

}

MyService.java

package com.example.testaidlserver;

import com.example.testaidl.MyInterface;

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

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		Log.i("MyService", "service onBind....");
		return new ImplAIDLService();
	}

	private class ImplAIDLService extends MyInterface.Stub {

		@Override
		public void testMethod() throws RemoteException {
			Log.i("MyService", "testMode invoked");
		}
	}
}

Manifest中加入MyService的注冊

<service  android:name="com.example.testaidlserver.MyService">
     <intent-filter>
           <action android:name="com.example.testaidlserver.MyService"/>
           <category android:name="android.intent.category.DEFAULT"/>
     </intent-filter>
</service>

以下是Client端的

技术分享

技术分享

aidl文件和server端的要一样

package com.example.testaidl;
interface MyInterface {
    void testMethod();
}

MainAcitvity的功能是,绑定server端的service。调用server端的方法

package com.example.testaidlclient;

import com.example.testaidl.MyInterface;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	private boolean mIsBinded = false;
	private MyInterface mInterface;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btn_bind = (Button)findViewById(R.id.btn_bind);
		btn_bind.setOnClickListener(this);
		Button btn_invoke = (Button)findViewById(R.id.btn_invoke);
		btn_invoke.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.btn_bind:
			if(mIsBinded) {
				unbindService(con);
			}else {
				bindService(new Intent("com.example.testaidlserver.MyService"), con, Context.BIND_AUTO_CREATE);
			}
			break;
		case R.id.btn_invoke:
			try {
				mInterface.testMethod();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			break;
		}
	}

	ServiceConnection con = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			mIsBinded = false;
			mInterface = null;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mIsBinded = true;
			mInterface = MyInterface.Stub.asInterface(service);
		}
	};

}

执行结果:

技术分享技术分享


AIDL调用指南

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/5323608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!