不依赖AIDL的跨进程通信

http://blog.csdn.net/lmj623565791/article/details/38461079

 

如果知道AIDL和binder的原理,可以简单写一个不依赖AIDL的跨进程通信

不知道的话可以看这里:http://www.cnblogs.com/qlky/p/6720405.html

 

先看AIDL要做什么: 在binderProxy的onTransact中接收数据,并返回结果。

在自定义service的stub中实现函数,并返回stub

所以如果只用binder,在自定义service重写onTransact就行了

package com.example.zhy_binder;  
  
import android.app.Service;  
import android.content.Intent;  
import android.os.Binder;  
import android.os.IBinder;  
import android.os.Parcel;  
import android.os.RemoteException;  
import android.util.Log;  
  
public class CalcPlusService extends Service  
{  
    private static final String DESCRIPTOR = "CalcPlusService";  
    private static final String TAG = "CalcPlusService";  
  
    public void onCreate()  
    {  
        Log.e(TAG, "onCreate");  
    }  
  
    @Override  
    public int onStartCommand(Intent intent, int flags, int startId)  
    {  
        Log.e(TAG, "onStartCommand");  
        return super.onStartCommand(intent, flags, startId);  
    }  
  
    public IBinder onBind(Intent t)  
    {  
        Log.e(TAG, "onBind");  
        return mBinder;  
    }  
  
    public void onDestroy()  
    {  
        Log.e(TAG, "onDestroy");  
        super.onDestroy();  
    }  
  
    public boolean onUnbind(Intent intent)  
    {  
        Log.e(TAG, "onUnbind");  
        return super.onUnbind(intent);  
    }  
  
    public void onRebind(Intent intent)  
    {  
        Log.e(TAG, "onRebind");  
        super.onRebind(intent);  
    }  
  
    private MyBinder mBinder = new MyBinder();  
  
    private class MyBinder extends Binder  
    {  
        @Override  
        protected boolean onTransact(int code, Parcel data, Parcel reply,  
                int flags) throws RemoteException  
        {  
            switch (code)  
            {  
            case 0x110:  
            {  
                data.enforceInterface(DESCRIPTOR);  
                int _arg0;  
                _arg0 = data.readInt();  
                int _arg1;  
                _arg1 = data.readInt();  
                int _result = _arg0 * _arg1;  
                reply.writeNoException();  
                reply.writeInt(_result);  
                return true;  
            }  
            case 0x111:  
            {  
                data.enforceInterface(DESCRIPTOR);  
                int _arg0;  
                _arg0 = data.readInt();  
                int _arg1;  
                _arg1 = data.readInt();  
                int _result = _arg0 / _arg1;  
                reply.writeNoException();  
                reply.writeInt(_result);  
                return true;  
            }  
            }  
            return super.onTransact(code, data, reply, flags);  
        }  
  
    };  
  
}

 

然后在客户端中需要序列化数据,并用transact把数据发送出去

package com.example.zhy_binder_client03;  
  
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.Toast;  
  
public class MainActivity extends Activity  
{  
  
    private IBinder mPlusBinder;  
    private ServiceConnection mServiceConnPlus = new ServiceConnection()  
    {  
        @Override  
        public void onServiceDisconnected(ComponentName name)  
        {  
            Log.e("client", "mServiceConnPlus onServiceDisconnected");  
        }  
  
        @Override  
        public void onServiceConnected(ComponentName name, IBinder service)  
        {  
  
            Log.e("client", " mServiceConnPlus onServiceConnected");  
            mPlusBinder = service;  
        }  
    };  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
    }  
  
    public void bindService(View view)  
    {  
        Intent intentPlus = new Intent();  
        intentPlus.setAction("com.zhy.aidl.calcplus");  
        boolean plus = bindService(intentPlus, mServiceConnPlus,  
                Context.BIND_AUTO_CREATE);  
        Log.e("plus", plus + "");  
    }  
  
    public void unbindService(View view)  
    {  
        unbindService(mServiceConnPlus);  
    }  
  
    public void mulInvoked(View view)  
    {  
  
        if (mPlusBinder == null)  
        {  
            Toast.makeText(this, "未连接服务端或服务端被异常杀死", Toast.LENGTH_SHORT).show();  
        } else  
        {  
            android.os.Parcel _data = android.os.Parcel.obtain();  
            android.os.Parcel _reply = android.os.Parcel.obtain();  
            int _result;  
            try  
            {  
                _data.writeInterfaceToken("CalcPlusService");  
                _data.writeInt(50);  
                _data.writeInt(12);  
                mPlusBinder.transact(0x110, _data, _reply, 0);  
                _reply.readException();  
                _result = _reply.readInt();  
                Toast.makeText(this, _result + "", Toast.LENGTH_SHORT).show();  
  
            } catch (RemoteException e)  
            {  
                e.printStackTrace();  
            } finally  
            {  
                _reply.recycle();  
                _data.recycle();  
            }  
        }  
  
    }  
      
    public void divInvoked(View view)  
    {  
  
        if (mPlusBinder == null)  
        {  
            Toast.makeText(this, "未连接服务端或服务端被异常杀死", Toast.LENGTH_SHORT).show();  
        } else  
        {  
            android.os.Parcel _data = android.os.Parcel.obtain();  
            android.os.Parcel _reply = android.os.Parcel.obtain();  
            int _result;  
            try  
            {  
                _data.writeInterfaceToken("CalcPlusService");  
                _data.writeInt(36);  
                _data.writeInt(12);  
                mPlusBinder.transact(0x111, _data, _reply, 0);  
                _reply.readException();  
                _result = _reply.readInt();  
                Toast.makeText(this, _result + "", Toast.LENGTH_SHORT).show();  
  
            } catch (RemoteException e)  
            {  
                e.printStackTrace();  
            } finally  
            {  
                _reply.recycle();  
                _data.recycle();  
            }  
        }  
  
    }  
} 

 

转载于:https://www.cnblogs.com/qlky/p/6725730.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值