• Android 跨进程通信


    Android中常用的跨进程通信方法有以下几种:
    Intent、Binder、AIDL、Messenger、ContentProvider。

    Intent

    可以通过Intent传递数据和消息,但是只能传递一些简单的数据类型,比如字符串、整数等。
    示例:

    1. 从一个应用程序发送一个字符串到另一个应用程序,例如:
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.example.app1", "com.example.app2.MainActivity"));
    intent.putExtra("key", "value");
    startActivity(intent);
    
    • 1
    • 2
    • 3
    • 4
    1. 从一个服务发送一个自定义类的对象到另一个服务,例如:
    IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(serviceConnection);
    Bundle bundle = new Bundle();
    bundle.putParcelable("key", myObject);
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.example.app1", "com.example.app2.MyService"));
    intent.putExtras(bundle);
    sendBroadcast(intent);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Binder

    是Android系统中的跨进程通信机制,它可以传递任意类型的对象,包括自定义类的对象。
    示例:

    1. 在一个服务中创建一个对象,并将其绑定到另一个服务的Binder接口上,例如:
    public class MyService extends Service {
        private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
            @Override
            public String getMessage() throws RemoteException {
                return "Hello from MyService";
            }
        };
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 在客户端应用程序中绑定到服务并调用其方法,例如:
    IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyAidlInterface myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                String message = myAidlInterface.getMessage();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    });
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.example.app1", "com.example.app2"));
    bindService(intent, connection, Context.BIND_AUTO_CREATE);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    AIDL

    是Android系统中的一种接口描述语言,用于定义服务端和客户端之间的接口。
    示例同上。

    Messenger

    是一种轻量级的通信方式,可以在不同的进程之间传递消息。
    示例:

    1. 在服务端创建一个Handler对象,并将其封装到一个Binder对象中,例如:
    public class MyService extends Service {
        private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
            @Override
            public String getMessage() throws RemoteException {
                return "Hello from MyService";
            }
        };
    
        private final Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Bundle bundle = msg.getData();
                String message = bundle.getString("message");
                Log.d(TAG, "Received message: " + message);
            }
        };
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    1. 在客户端应用程序中绑定到服务并调用其方法,例如:
    IMyAidlInterface myService = IMyAidlInterface.Stub.asInterface(new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyAidlInterface myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                Message message = Message.obtain(null, 0, null, myAidlInterface.getMessage());
                myService.mHandler.sendMessage(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    });
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.example.app1", "com.example.app2"));
    bindService(intent, connection, Context.BIND_AUTO_CREATE);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    ContentProvider

    是一种特殊的IContentObserver,用于在不同的应用程序之间共享数据。

    以下是一个简单的示例:在主进程中创建一个ContentProvider,然后在子进程中通过ContentResolver访问这个ContentProvider来获取数据。

    // 在主进程中注册ContentProvider
    public class MyApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            ContentProvider myContentProvider = new MyContentProvider();
            registerContentProvider(myContentProvider);
        }
        
        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public String getType(Uri uri) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public Uri insert(Uri uri, ContentValues values) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    // 在子进程中通过ContentResolver访问ContentProvider获取数据
    public class MyActivity extends Activity {
        private static final String AUTHORITY = "com.example.myapp.provider";
        private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + AUTHORITY);
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Cursor cursor = getContentResolver().query(BASE_CONTENT_URI, null, null, null, null);
            while (cursor.moveToNext()) {
                // TODO: Do something with the data in the cursor.
            }
            cursor.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    外汇天眼:FCA 已向交易应用程序运营商发出警告,要求其停止交易游戏化
    【排序专题】不会吧,不会吧居然还有人不懂排序算法?有彩蛋哦
    Postman接口测试,全网最详细教程
    Mybatis的优缺点
    linux下C++开发环境搭建
    [Dubbo3.0.8源码解析系列]-26-消费者一个服务是如何通过RPC调用到提供者的
    阿里云/腾讯云幻兽帕鲁服务器为什么更新/重启之后,服务器存档没了?
    【面试系列】Java面试知识篇(一)
    GIS跟踪监管系统单元信息更新
    【C++笔记】第二十六篇 常用算法
  • 原文地址:https://blog.csdn.net/qq_33548747/article/details/138161211