先上最终效果图:
1.首先新建AIDL文件
// IAdditionService.aidl
package com.android.hellosumaidl;
// Declare any non-default types here with import statements
interface IAdditionService {
int add(in int value1, in int value2);
}
Android studio会在app\build\generated\aidl_source_output_dir\debug\compileDebugAidl\out\com\android\hellosumaidl下自动生成对应的java文件:
IAdditionService.java
2.Service实现
package com.android.hellosumaidl;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
/*
* This class exposes the service to client
*/
public class AdditionService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
Binder mBinder = new IAdditionService.Stub() {
@Override
public int add(int value1, int value2) throws RemoteException {
return value1+value2;
}
};
@Override
public void onDestroy() {
super.onDestroy();
}
}
3.Client实现
package com.android.hellosumaidl;
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;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
public class HelloSumAidlActivity extends Activity {
IAdditionService service;
AdditionServiceConnection connection;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initService();
Button buttonCalc = (Button)findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView)findViewById(R.id.result);
EditText value1 = (EditText)findViewById(R.id.value1);
EditText value2 = (EditText)findViewById(R.id.value2);
@Override
public void onClick(View v) {
int v1, v2, res = -1;
v1 = Integer.parseInt(value1.getText().toString());
v2 = Integer.parseInt(value2.getText().toString());
try {
res = service.add(v1, v2);
} catch (RemoteException e) {
e.printStackTrace();
}
result.setText(Integer.valueOf(res).toString());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseService();
}
/*
* This inner class is used to connect to the service
*/
class AdditionServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder boundService) {
service = IAdditionService.Stub.asInterface((IBinder)boundService);
Toast.makeText(HelloSumAidlActivity.this, "Service connected", Toast.LENGTH_LONG).show();
}
public void onServiceDisconnected(ComponentName name) {
service = null;
Toast.makeText(HelloSumAidlActivity.this, "Service disconnected", Toast.LENGTH_LONG).show();
}
}
/*
* This function connects the Activity to the service
*/
private void initService() {
connection = new AdditionServiceConnection();
Intent i = new Intent();
i.setClassName("com.android.hellosumaidl", com.android.hellosumaidl.AdditionService.class.getName());
boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);
}
/*
* This function disconnects the Activity from the service
*/
private void releaseService() {
unbindService(connection);
connection = null;
}
}