.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
/** Request the process ID of this service, to do evil things with it. */
public int getPid() throws android.os.RemoteException;
/** Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, java.lang.String aString) throws android.os.RemoteException;
}
?
?
AIDLService.java: ?
package com.example.boundservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class AIDLService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
@Override
public int getPid() throws RemoteException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString)
throws RemoteException {
// TODO Auto-generated method stub
}
};
}
AIDLActivity.java: ?
?
package com.example.boundservice;
import com.example.boundservice.IRemoteService.Stub;
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.widget.Toast;
public class AIDLActivity extends Activity implements ServiceConnection{
private Boolean mBound = false;
private IRemoteService iRemoteServe;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
bindService(new Intent(AIDLActivity.this,AIDLService.class), this, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(!mBound) {
unbindService(this);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aidl_activity);
findViewById(R.id.button_show_aidl_result).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
Toast.makeText(getApplicationContext(), +iRemoteServe.getPid(), Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
iRemoteServe = Stub.asInterface(arg1);
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
}
AIDLActivity Toast一个“0”,为AIDLService中getPid return 的0。 ?
下面介绍如何传递对象。
Passing Objects over IPC
?