import com.example.aidlserver.aidl.DataService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class TestService extends Service {
@Override
public IBinder onBind(Intent intent) {
return binder;
}
private final DataService.Stub binder = new DataService.Stub() {
@Override
public int getData(String type) throws RemoteException {
return 5;
}
@Override
public String getTime() throws RemoteException {
return “2016-01-23”;
}
};
}
③AndroidManifest.xml声明Service,采用隐式Intent供Client调用:
2.AIDLClient
①将AIDLServer中的aidl文件和其包复制到AIDLClient下。
②通过隐式Intent和ServiceConnection 绑定Service之后就可以使用AIDLServer中的数据接口:
package com.example.aidlclient;
import com.example.aidlserver.aidl.DataService;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btnBind, btnUnBind, btnData, btnTime;
private DataService dataService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnBind = (Button) findViewById(R.id.btnBind);
btnBind.setOnClickListener(new MyOnClickListener());
btnUnBind = (Button) findViewById(R.id.btnUnBind);
btnUnBind.setOnClickListener(new MyOnClickListener());
btnData = (Button) findViewById(R.id.btnData);
btnData.setOnClickListener(new MyOnClickListener());
btnTime = (Button) findViewById(R.id.btnTime);
btnTime.setOnClickListener(new MyOnClickListener());
}
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.v(“ZMS”, “ServiceConnection: onServiceDisconnected”);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
dataService = DataService.Stub.asInterface(service);
Log.v(“ZMS”, “ServiceConnection: onServiceConnected”);
}
};
class MyOnClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnBind:
// Intent intent = new Intent(DataService.class.getName());
Intent intentBind = new Intent(
“com.example.aidlserver.aidl.DataService”);
boolean bindSuccess = bindService(intentBind,
serviceConnection, Context.BIND_AUTO_CREATE);
Toast.makeText(MainActivity.this, “bindSuccess:” + bindSuccess,
Toast.LENGTH_SHORT).show();
break;
case R.id.btnUnBind:
if (dataService != null) {
unbindService(serviceConnection);
} else {
Log.e(“ZMS”, “dataService is NULL”);
}
break;
case R.id.btnData:
try {
int data = dataService.getData(“”);
Toast.makeText(MainActivity.this, “” + data,
Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.btnTime:
try {
String time = dataService.getTime();
Toast.makeText(MainActivity.this, “” + time,
Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
default:
break;
}