不依赖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