package com.example.advanceandroid.aidl;
interface ILogin {
String login();
}
package com.example.advanceandroid.aidl;
public interface ILogin extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements
com.example.advanceandroid.aidl.ILogin
{
private static final java.lang.String DESCRIPTOR = "com.example.advanceandroid.aidl.ILogin";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.advanceandroid.aidl.ILogin
* interface, generating a proxy if needed.
*/
public static com.example.advanceandroid.aidl.ILogin asInterface(android.os.IBinder obj)
{
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.example.advanceandroid.aidl.ILogin))) {
return ((com.example.advanceandroid.aidl.ILogin) iin);
}
return new com.example.advanceandroid.aidl.ILogin.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_login: { // 1、登录请求,执行的是this.login();
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.login();
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.example.advanceandroid.aidl.ILogin
{
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 java.lang.String login() throws android.os.RemoteException // 2、Proxy中的login,通过Binder机制实现IPC
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_login, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_login = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.lang.String login() throws android.os.RemoteException;
}
/**
* AIDL服务端接口,LoginStubImpl实现了ILogin接口.
*
* @author mrsimple
*/
public class LoginService extends Service {
/**
*
*/
IBinder mBinder = new LoginStubImpl();
/**
* @author mrsimple
*/
class LoginStubImpl extends Stub {
@Override
public String login() throws RemoteException {
return "这是从 " + this.getClass().getName() + " 返回的字符串";
}
}
/*
* 返回Binder实例,即实现了ILogin接口的Stub的子类,这里为LoginStubImpl
* [url=home.php?mod=space&uid=133757]@see[/url] android.app.Service#onBind(android.content.Intent)
*/
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
IBinder mBinder = new LoginStubImpl();
<!-- aidl server service -->
<service android:name="com.example.advanceandroid.aidl.LoginService" >
<intent-filter>
<action android:name="com.example.advanceandroid.aidl.LoginService" />
</intent-filter>
</service>
ServiceConnection mLoginConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d("", "### aidl disconnected.");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d("", "### aidl onServiceConnected. service : " + service.getClass().getName());
ILogin login = Stub.asInterface(service);
Log.d("", "### after asInterface : " + login.getClass().getName());
try {
Log.d("", "### login : " + login.login());
// Toast.makeText(MainActivity.this, "onServiceConnected : " +
// login.login(),
// Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
@Override
protected void onResume() {
super.onResume();
// 服务端的action
Intent aidlIntent = new Intent("com.example.advanceandroid.aidl.LoginService");
bindService(aidlIntent, mLoginConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// unbind
unbindService(mLoginConnection);
}
09-02 10:40:54.662: D/(9589): ### aidl onServiceConnected. service : android.os.BinderProxy 09-02 10:40:54.662: D/(9589): ### after asInterface : com.example.advanceandroid.aidl.ILogin$Stub$Proxy 09-02 10:40:54.662: D/(9589): ### login : 这是从 com.example.advanceandroid.aidl.LoginService$LoginStubImpl 返回的字符串
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有