GithubHelp home page GithubHelp logo

mangoipc's Introduction

MangoIPC

博客描述 :https://blog.csdn.net/qq_30993595/article/details/91467494

你还在为了Android应用进程间通信发愁吗?

还在为了怎么编写AIDL文件而烦吗?

还在为了定义各种各样的方法及参数返回值对象心塞吗?

还在为了数据传输的序列化和反序列化挠头吗?

还在为了定义各种各样的Service闹心吗?

一个简单可靠的基于Android应用的跨进程通信框架在这里,可使用框架提供的RemoteService作为服务端,也可自定义Service,使用简单,与业务解耦

Step 1

  • 在项目根目录build.gradle中添加

    allprojects {
        repositories {
            ......
            maven { url 'https://jitpack.io' }
        }
    }
  • 如果服务端和客户端在一个模块,在模块下的build.gradle中添加

    dependencies {
        implementation 'com.github.Mangosir:MangoIPC:1.0.1'
    }

    如果服务端和客户端不在同一个模块,那在各自的build.gradle中都得加上

Step 2

服务端自定义Service,客户端先绑定服务,再发送请求

服务端:

public class MyRemoteService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new IRemoteService.Stub(){

            @Override
            public IPCResponse sendRequest(IPCRequest request) throws RemoteException {

                return new IPCResponse("hello","执行方法成功",true);
            }
        };
    }
}

客户端:

        private String mRemotePackageName = "com.mango.ipc";
        private String mRemoteServiceName = "com.mango.ipc.MyRemoteService";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            IPCMango.getDefault().bind(this,mRemotePackageName,mRemoteServiceName);

        }
    
        public void sendMyData(View view){
            //发送请求给服务端自定义的远程Service
            IPCResponse ipcResponse = IPCMango.getDefault().sendRequest(
                IPCMango.getDefault().buildRequest(0,null,null), 
                mRemoteServiceName);
        }

	public void onDestory(){
            super.onDestory();
            IPCMango.getDefault().unBind(this,mRemoteServiceName);
	}

Step 3

上面两步做完你就可以完成进程间通信了 当然你也可以使用框架提供的RemoteService,更加通用,使得业务与Service解耦,面向接口编程

服务端:

  • 定义一个业务接口,规定与客户端通信规则,比如

    public interface IData {
    
        List sendData(String params);
    
        Object getStudent(String name);
    
    }
  • 实现该接口,处理客户端请求;添加运行时注解,注解值需要与后面客户端中的保持一致;获取实例的方法必须是public static且方法名为getDefault

    @RequestHandler("IData")
    public class DataManager implements IData {
    
        private static final DataManager ourInstance = new DataManager();
    
        public static DataManager getDefault(String params) {
            return ourInstance;
        }
    
        private DataManager() {}
        
        @Override
        public List sendData(String params) {
            List<String> list = new ArrayList<>();
            list.add("1");
            list.add("2");
            list.add("3");
            return list;
        }
    
        @Override
        public Student getStudent(String name) {
            return new Student(name);
        }
    }
  • 在某个时刻将上面的处理客户端请求的Class进行注册

    IPCMango.getDefault().register(DataManager.class);

    记得不需要的时候解除注册

    IPCMango.getDefault().unRegister(DataManager.class);
  • 在AndroidManifest中注册RemoteService

    <service android:name="com.mango.ipcore.handler.RemoteService"
                android:exported="true"></service>

客户端:

  • 在Activity的onCreate中绑定远程Service
    //服务端应用的包名
    private String mRemotePackageName = "com.mango.ipc";
    IPCMango.getDefault().bind(this,mRemotePackageName);
    在Activity销毁时解绑Service
    
    IPCMango.getDefault().unBind(this);
  • 新建一个接口,名字不需要与服务端接口一样,要求接口中的方法必须与其一致,包括注解值
    @RequestHandler("IData")
    public interface IData {
    
        List sendData(String params);
    
        Object getStudent(String name);
    
    }
    
  • 到这里就是发起请求给远程Service
        public void sendData(View v){
            //如果服务端会执行耗时操作,这里需要放在子线程
            //发送请求给框架提供的RemoteService
            if (iData == null) {
                iData = IPCMango.getDefault()
                            .loadRequestHandler(IData.class,"伙计");
            }
            if (iData != null) {
                List list = iData.sendData("小米");
                Object student = iData.getStudent("阿菜");
            } 
        }

mangoipc's People

Contributors

hellofish-2016 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.