AIDL:双向传递
Service:
1.interface FirstAIDL{
String getName();
void setUser(String name, int age, char sex);
}
==>Rebuild
2.public class AIDLService extends Service {
private FirstAIDL.Stub stub = new FirstAIDL.Stub() {
... // get、set
};
public IBinder onBind(Intent intent) { return stub; }
}
注册Service:
<Service android:name=".AIDLService">
<intent-filter>
<action android:name="service名字(全小写)"/>
</intent-filter>
</Service>
Client:
1.project拷贝Service aidl包 ==>Rebuild
2.MainActivity——>onCreate(){
//通过服务的隐式意图跳转方式开启服务
Intent intent = new Intent("service的action名");
intent.setPackage("service应用的包名");
bindService(intent, new MyConnection(),BIND_AUTO_CREATE);
}
class MyConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder service) {
FirstAIDL firstAidl = FirstAIDL.Stub.asInterface(service);
try {
String username = firstAidl.getName();
firstAidl.setUser("小米", 18, 'm');
} catch(...) {}
}
}