[置顶] Android中ail的作用和使用说明

为什么要使用aidl

Android系统中的进程之间不能共享内存,为了使其他的应用程序也可以访问本应用程序提供的服务的远程调用。这么说有有些太官方了,简单的理解就是新浪微博的SSO授权快速登录。你本地有Cilent和Service两个应用,使用aidl之后Client应用可以直接调用Service应用中的方法了。


使用注意

本地创建cilent和service两个应用,aidl文件必须要同包、同名、同方法。

Demo下载地址

http://download.csdn.net/detail/aaren_jiang/7494661

共有的.aidl文件

package com.chiang.aidl;

interface RemoteSSO {
	String getPackageName();
	String getActivityName(String param);
}

Service端

这里是JAVA代码
public class AIDLService extends Service {
	private static final String TAG = "AIDLService";

	private void Log(String str) {
		Log.i(TAG, "----------" + str + "----------");
	} 

	public void onCreate() {
		Log("service created");
	}

	public void onStart(Intent intent, int startId) {
		Log("service started id = " + startId);
	}

	public IBinder onBind(Intent t) {
		Log("service on bind");
		return mBinder;
	}

	public void onDestroy() {
		Log("service on destroy");
		super.onDestroy();
	}

	public boolean onUnbind(Intent intent) {
		Log("service on unbind");
		return super.onUnbind(intent);
	}

	public void onRebind(Intent intent) {
		Log("service on rebind");
		super.onRebind(intent);
	}

	/**
	 * Service实现该方法,给远程Client调用
	 */
	private final RemoteSSO.Stub mBinder = new RemoteSSO.Stub() {

		@Override
		public String getPackageName() throws RemoteException {
			Log.e(TAG, "remote call from client! current thread id = " + Thread.currentThread().getId());
			return "调用service获取包名";
		}

		@Override
		public String getActivityName(String param) throws RemoteException {
			Log.e(TAG, "remote call from client! current thread id = " + Thread.currentThread().getId() +"  param:"+param);
			return "调用service获取activity名";
		}
	};
}
AndroidMainifest.xml文件的配置,只需要在application标签里增加service标签即可
<service
            android:name=".AIDLService"
            android:exported="true" >
            <intent-filter>

                <!-- 这里的action是给cilent调用的标识 -->
                <action android:name="com.chiang.aidlservice.aidl" />

            </intent-filter>
        </service>

Clinet端

/**
 * 注意.aidl文件要和被调用的.aidl文件要是一样的包名、类名、方法名,否则你调用会错误
 * @author JiangHao
 *
 */
public class MainActivity extends Activity implements OnClickListener {
	
	public static final String TAG = "AIDL";
	private RemoteSSO remoteSSO;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//根据service的action名称来调用
		Intent intent = new Intent("com.chiang.aidlservice.aidl");
		bindService(intent, connection, Context.BIND_AUTO_CREATE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.callRemote:{//远程调用
			if(remoteSSO==null){
				Toast.makeText(this, "等待连接...", Toast.LENGTH_SHORT).show();
				return;
			}
			try {
				Toast.makeText(this, "---"+remoteSSO.getActivityName("我去")+"---", Toast.LENGTH_SHORT).show();
			} catch (RemoteException e) {
				e.printStackTrace();
				Toast.makeText(this, "---错误---", Toast.LENGTH_SHORT).show();
			}
			break;
		}
		case R.id.cancalCall:{//注销
			unbindService(connection);
			break;
		}
		}
		
	}
	
	ServiceConnection connection=new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.i(TAG, "注销连接!");
			remoteSSO = null;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i(TAG, "连接成功!");
			remoteSSO = RemoteSSO.Stub.asInterface(service);
		}
	};

}


你可能感兴趣的:(android,aidl,android远程调用)