android binder ipc

心鑫發表於2013-11-27

服務端

IDateTimeService.aidl 檔案寫法 與普通java類相同 

package com.example.visualizertest;
interface IDateTimeService 
{
 String getCurrentDateTime(in String format) ;
}

 沒問題的話 會在Gen目錄下生成 同名的.java檔案

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: D:\\ProjectGitReposit\\VisualizerTest\\src\\com\\example\\visualizertest\\IDateTimeService.aidl
 */
package com.example.visualizertest;
public interface IDateTimeService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.visualizertest.IDateTimeService
{
private static final java.lang.String DESCRIPTOR = "com.example.visualizertest.IDateTimeService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.example.visualizertest.IDateTimeService interface,
 * generating a proxy if needed.
 */
public static com.example.visualizertest.IDateTimeService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.visualizertest.IDateTimeService))) {
return ((com.example.visualizertest.IDateTimeService)iin);
}
return new com.example.visualizertest.IDateTimeService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getCurrentDateTime:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
java.lang.String _result = this.getCurrentDateTime(_arg0);
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.example.visualizertest.IDateTimeService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public java.lang.String getCurrentDateTime(java.lang.String format) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(format);
mRemote.transact(Stub.TRANSACTION_getCurrentDateTime, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getCurrentDateTime = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.lang.String getCurrentDateTime(java.lang.String format) throws android.os.RemoteException;
}

寫個Service 
package com.example.visualizertest;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class DateTimeService extends Service {


	public IBinder onBind(Intent arg0) {
		return new IDateTimeService.Stub() {

			public String getCurrentDateTime(String format) throws RemoteException {
				return new SimpleDateFormat(format).format(new Date());
			}
		};
	}
}

配置檔案中配置

<service android:name=".DateTimeService" >
            <intent-filter>
                <action android:name="com.example.visualizertest.DateTimeService">
                </action>
            </intent-filter>
        </service>

客戶端 :

建立一個與服務端 aidl相同的包名 相同的aidl檔案 gen下自動生成.java檔案與服務端相同。
否則會報ERROR/AndroidRuntime(716): java.lang.SecurityException: Binder invocation to an incorrect interface這個錯誤

使用service與在同程式 相同
package com.example.binder;

import android.app.Activity;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.visualizertest.IDateTimeService;

public class MainActivity2 extends Activity {
	class MyServiceConnection implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			printLog("bind success");
			is=IDateTimeService.Stub.asInterface(service);

		}

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

		}

	}

	TextView tv1;
	Button b1;
	Button b2;
	MyServiceConnection sc;
	IDateTimeService is;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv1 = (TextView) findViewById(R.id.textView1);
		b1 = (Button) findViewById(R.id.button1);
		b2 = (Button) findViewById(R.id.button2);
		b1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent("com.example.visualizertest.DateTimeService");
				sc = new MyServiceConnection();
				bindService(intent, sc, BIND_AUTO_CREATE);

			}
		});
		b2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					String s=is.getCurrentDateTime("yyyy-MM-hh");
					printLog("return-->"+s);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
	}
	@Override
	protected void onDestroy() {
		unbindService(sc);
		super.onDestroy();
	}
	private void printLog(String string) {
		Log.d("MainActivity2", string);

	}
}

結:aidl介面檔案相同

相關文章