package com.example.serviceaidlsimpletest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class RemoteService extends Service {
private static final String TAG = "RemoteService";
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
Log.i(TAG,TAG + " onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,TAG + " onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i(TAG,TAG + " onDestroy");
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG,TAG + " onUnbind");
return super.onUnbind(intent);
}
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid(){
return 1;
}
};
}
package com.example.serviceaidlsimpletest;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ServiceAIDLActivity extends Activity {
protected static final String TAG = "RemoteService";
Button aidlStartBtn;
Button aidlFucBtn;
Button aidlShutBtn;
IRemoteService mIRemoteService;
private ServiceConnection serviceconn = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, TAG + " onServiceConnected");
Toast.makeText(getApplicationContext(), "远程服务连上", Toast.LENGTH_SHORT).show();
mIRemoteService = IRemoteService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "Service has unexpectedly disconnected");
Toast.makeText(getApplicationContext(), "远程服务异常断开", Toast.LENGTH_SHORT).show();
mIRemoteService = null;
}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service_aidl);
aidlStartBtn = (Button)findViewById(R.id.aidlBtn);
aidlStartBtn.setEnabled(true);
aidlStartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ServiceAIDLActivity.this,RemoteService.class);
bindService(intent, serviceconn, Context.BIND_AUTO_CREATE);
aidlStartBtn.setEnabled(false);
aidlShutBtn.setEnabled(true);
}
});
aidlFucBtn = (Button)findViewById(R.id.aidlFucBtn);
aidlFucBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if(mIRemoteService != null){
int i = mIRemoteService.getPid();
Log.i(TAG, TAG + " 函数调用");
Toast.makeText(getApplicationContext(), "函数调用 --> " + i, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "函数调用 失败,先打开远程Service连接", Toast.LENGTH_SHORT).show();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
aidlShutBtn = (Button)findViewById(R.id.aidlShutBtn);
aidlShutBtn.setEnabled(false);
aidlShutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(serviceconn);
Toast.makeText(getApplicationContext(), "远程服务断开", Toast.LENGTH_SHORT).show();
aidlStartBtn.setEnabled(true);
aidlShutBtn.setEnabled(false);
mIRemoteService = null;
}
});
}
}
IRemoteService.aidl
package com.example.serviceaidlsimpletest;
interface IRemoteService {
int getPid();
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.serviceaidlsimpletest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.serviceaidlsimpletest.ServiceAIDLActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.serviceaidlsimpletest.RemoteService">
</service>
</application>
</manifest>
便于理解贴上自动生成的文件
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: D:\\TestEclipse\\ServiceAIDLSimpleTest\\src\\com\\example\\serviceaidlsimpletest\\IRemoteService.aidl
*/
package com.example.serviceaidlsimpletest;
public interface IRemoteService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.serviceaidlsimpletest.IRemoteService
{
private static final java.lang.String DESCRIPTOR = "com.example.serviceaidlsimpletest.IRemoteService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.serviceaidlsimpletest.IRemoteService interface,
* generating a proxy if needed.
*/
public static com.example.serviceaidlsimpletest.IRemoteService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.serviceaidlsimpletest.IRemoteService))) {
return ((com.example.serviceaidlsimpletest.IRemoteService)iin);
}
return new com.example.serviceaidlsimpletest.IRemoteService.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_getPid:
{
data.enforceInterface(DESCRIPTOR);
int _result = this.getPid();
reply.writeNoException();
reply.writeInt(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.example.serviceaidlsimpletest.IRemoteService
{
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 int getPid() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getPid, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getPid = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public int getPid() throws android.os.RemoteException;
}