• 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
  • 相关阅读:
    java数据结构与算法刷题-----LeetCode1854:人口最多的年份
    javascript基本语法(持续补充)
    代码解析 最短路径 排序
    基于矩阵分解算法的智能Steam游戏AI推荐系统——深度学习算法应用(含python、ipynb工程源码)+数据集(一)
    IDEA工具第二篇:自定义Java方法注释模板 | 京东云技术团队
    vulhub中Apache Log4j2 lookup JNDI 注入漏洞(CVE-2021-44228)
    C#使用PaddleOCR进行图片文字识别✨
    艺术VQA的数据集与基线baselines
    hystart++ 出炉
    92.Linux的僵死进程以及处理方法
  • 原文地址:https://blog.csdn.net/qq_33548747/article/details/138161211