美文网首页我爱编程
Android IPC(三)选用合适的IPC方式

Android IPC(三)选用合适的IPC方式

作者: 锋Plus | 来源:发表于2018-04-14 19:57 被阅读48次

    Bundle

    四大组件中的三大组件(Activity、Service、Receiver)都是支持在Intent中传递Bundle数据的,由于Bundle实现了Parcelable接口,所以它可以方便地在不同地进程间传输。传输的数据必须能够被序列化,比如基本类型、实现了Parcelable接口的对象、实现了Serializable接口的对象以及一些Android支持的特殊对象,具体内容可以看Bundle这个类,就可以看到所有他支持的类型。

    使用文件共享

    两个进程通过读/写同一个文件来交换数据,比如A进程把数据写入文件,B进程通过读取这个文件来获取数据。

    下面举个简答的栗子

        /**
         * MainActivity中
         * 序列化对象
         */
        private void persistToFile() {
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    User user = new User(1, "hello world", false);
                    File dir = new File(MyConstants.CHAPTER_02_PATH);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    File cachedFile = new File(MyConstants.CACHE_FILE_PATH);
                    ObjectOutputStream objectOutputStream = null;
                    try {
                        // 序列化操作
                        objectOutputStream = new ObjectOutputStream(
                                new FileOutputStream(cachedFile));
                        objectOutputStream.writeObject(user);
                        Log.d(TAG, "persist user:" + user);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        MyUtils.close(objectOutputStream);
                    }
                }
            }).start();
        }
    
        /**
         *  SecendActivity中
         * 从文件中恢复对象,反序列化
         */
        private void recoverFromFile() {
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    User user;
                    File cachedFile = new File(MyConstants.CACHE_FILE_PATH);
                    if (cachedFile.exists()) {
                        ObjectInputStream objectInputStream = null;
                        try {
                            objectInputStream = new ObjectInputStream(
                                    new FileInputStream(cachedFile));
                            user = (User) objectInputStream.readObject();
                            Log.d(TAG, "recover user:" + user);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } finally {
                            MyUtils.close(objectInputStream);
                        }
                    }
                }
            }).start();
        }
    
    存对象.png 取对象.png

    在SecendActivity中成功地从文件恢复了之前存储地User对象的内容(内容一样,但它们本质上还是两个对象)。

    由于Android系统基于Linux,使得其并发读/写文件可以没有限制地进行,甚至两个线程同时对同一个文件进行读写操作都是允许的,所以这在特殊情况有可能出问题。要尽量避免并发写这种情况或者考虑使用线程同步来限制多个线程的写操作。

    当然SharedPreferences是个特例。从本质上来,SharedPreferences也属于文件的一种,但是由于系统对他的读/写有一定的缓存策略,即在内存中会有一份SharedPreferences文件的缓存,因此在多线程模式下,系统对它的读/写就变得不可靠,当面对高并发的读/写访问,SharedPreferences有很大几率会丢失数据,因此,不建议在进程间通讯中使用SharedPreferences

    使用Messenger

    Messenger是一种轻量级的IPC方案,它的底层实现是AIDL。

    //构造方法
        public Messenger(IBinder target) {
            mTarget = IMessenger.Stub.asInterface(target);
        }
    

    Messenger的使用方法很简单,它对AIDL做了封装,使得我们可以更简便地进行进程间通信。同时,由于它一次处理一个请求,因此在服务端我们不用考虑线程同步的问题,这是因为服务端中不存在并发执行的情形。

    实现一个Messenger有如下几个步骤,分为服务端和客户端。

    服务端进程

    首先,我们需要在服务端创建一个Service来处理客户端的连接请求,同时创建一个Handler并通过它来创建Messenger对象,然后在ServiceonBind中返回这个Messenger对象底层的Binder即可。

    客户端进程

    客户端进程中,首先要绑定服务端的Service,绑定成功后用服务端返回的IBinder对象创建一个Messenger,通过这个Messenger就可以向服务端发送消息了,发送消息类型为Message对象。如果需要服务端能够回应客户端,就和服务端一样,我们还需要创建一个Handler并创建一个新的Messenger,并把这个Messenger对象通过MessagereplyTo参数传递给服务端,服务端通过这个replyTo参数就可以回应客户端。

    说了这么多,直接上代码

    // 服务端实现
    public class MessengerService extends Service {
    
        private static final String TAG = "MessengerService";
    
        private static class MessengerHandler extends Handler {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MyConstants.MSG_FROM_CLIENT:
                    Log.i(TAG, "receive msg from Client:" + msg.getData().getString("msg"));
                    Messenger client = msg.replyTo;
                    Message relpyMessage = Message.obtain(null, MyConstants.MSG_FROM_SERVICE);
                    Bundle bundle = new Bundle();
                    bundle.putString("reply", "嗯,你的消息我已经收到,稍后会回复你。");
                    relpyMessage.setData(bundle);
                    try {
                        client.send(relpyMessage);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    super.handleMessage(msg);
                }
            }
        }
    
        private final Messenger mMessenger = new Messenger(new MessengerHandler());
    
        @Override
        public IBinder onBind(Intent intent) {
            return mMessenger.getBinder();
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }
    
    }
    

    注册Service,让其运行在单独的进程中:

            <service
                android:name=".messenger.MessengerService"
                android:process=":remote" >
                <intent-filter>
                    <action android:name="com.chong.MessengerService.launch" />
                </intent-filter>
            </service>
    
    //客户端实现
    public class MessengerActivity extends Activity {
    
        private static final String TAG = "MessengerActivity";
    
        private Messenger mService;
        private Messenger mGetReplyMessenger = new Messenger(new MessengerHandler());
        
        private static class MessengerHandler extends Handler {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MyConstants.MSG_FROM_SERVICE:
                    Log.i(TAG, "receive msg from Service:" + msg.getData().getString("reply"));
                    break;
                default:
                    super.handleMessage(msg);
                }
            }
        }
    
        private ServiceConnection mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                mService = new Messenger(service);
                Log.d(TAG, "bind service");
                Message msg = Message.obtain(null, MyConstants.MSG_FROM_CLIENT);
                Bundle data = new Bundle();
                data.putString("msg", "hello, this is client.");
                msg.setData(data);
                msg.replyTo = mGetReplyMessenger;
                try {
                    mService.send(msg);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
    
            public void onServiceDisconnected(ComponentName className) {
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_messenger);
            Intent intent = new Intent("com.chong.MessengerService.launch");
            bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        }
        
        @Override
        protected void onDestroy() {
            unbindService(mConnection);
            super.onDestroy();
        }
    }
    

    可以看出,在Messenger中进行数据传递必须将数据放入Message中,而Messenger和Message都实现了Parcelable接口,因此可以跨进程传输。通过Messenger来传输Message,Message中能使用的载体只有what、arg1、arg2、Bundle以及replyTo。Message中的另一个字段object在同一个进程中是实用的,但是在进程间通信的时候,也仅仅是系统提供的实现了Parcelable接口的对象才能通过它来传输。

    Messenger的工作原理

    使用ContentProvider

    ContentProvider是Android中提供的专门用于不同应用间进行数据共享的方式,从这一点来看,它天生就适合进程间通信。和Messenger一样,ContentProvider的底层实现也是Binder,并且系统已经为我们做了封装。

    这里就简单实现一个Demo

    首先创建一个SQLite管理类来提供数据

    public class DbOpenHelper extends SQLiteOpenHelper {
    
        private static final String DB_NAME = "book_provider.db";
        public static final String BOOK_TABLE_NAME = "book";
        public static final String USER_TALBE_NAME = "user";
    
        private static final int DB_VERSION = 3;
    
        private String CREATE_BOOK_TABLE = "CREATE TABLE IF NOT EXISTS "
                + BOOK_TABLE_NAME + "(_id INTEGER PRIMARY KEY," + "name TEXT)";
    
        private String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS "
                + USER_TALBE_NAME + "(_id INTEGER PRIMARY KEY," + "name TEXT,"
                + "sex INT)";
    
        public DbOpenHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_BOOK_TABLE);
            db.execSQL(CREATE_USER_TABLE);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO ignored
        }
    
    }
    

    ContentProvider代码

    public class BookProvider extends ContentProvider {
    
        private static final String TAG = "BookProvider";
    
        public static final String AUTHORITY = "com.chong.chapter02.book.provider";
    
        public static final Uri BOOK_CONTENT_URI = Uri.parse("content://"
                + AUTHORITY + "/book");
        public static final Uri USER_CONTENT_URI = Uri.parse("content://"
                + AUTHORITY + "/user");
    
        public static final int BOOK_URI_CODE = 0;
        public static final int USER_URI_CODE = 1;
        private static final UriMatcher sUriMatcher = new UriMatcher(
                UriMatcher.NO_MATCH);
    
        static {
            sUriMatcher.addURI(AUTHORITY, "book", BOOK_URI_CODE);
            sUriMatcher.addURI(AUTHORITY, "user", USER_URI_CODE);
        }
    
        private Context mContext;
        private SQLiteDatabase mDb;
    
        @Override
        public boolean onCreate() {
            Log.d(TAG, "onCreate, current thread:"
                    + Thread.currentThread().getName());
            mContext = getContext();
            initProviderData();
            return true;
        }
    
        private void initProviderData() {
            mDb = new DbOpenHelper(mContext).getWritableDatabase();
            mDb.execSQL("delete from " + DbOpenHelper.BOOK_TABLE_NAME);
            mDb.execSQL("delete from " + DbOpenHelper.USER_TALBE_NAME);
            mDb.execSQL("insert into book values(3,'Android');");
            mDb.execSQL("insert into book values(4,'Ios');");
            mDb.execSQL("insert into book values(5,'Html5');");
            mDb.execSQL("insert into user values(1,'jake',1);");
            mDb.execSQL("insert into user values(2,'jasmine',0);");
        }
    
        @Override
        public Cursor query(Uri uri, String[] projection, String selection,
                String[] selectionArgs, String sortOrder) {
            Log.d(TAG, "query, current thread:" + Thread.currentThread().getName());
            String table = getTableName(uri);
            if (table == null) {
                throw new IllegalArgumentException("Unsupported URI: " + uri);
            }
            return mDb.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);
        }
    
        @Override
        public String getType(Uri uri) {
            Log.d(TAG, "getType");
            return null;
        }
    
        @Override
        public Uri insert(Uri uri, ContentValues values) {
            Log.d(TAG, "insert");
            String table = getTableName(uri);
            if (table == null) {
                throw new IllegalArgumentException("Unsupported URI: " + uri);
            }
            mDb.insert(table, null, values);
            mContext.getContentResolver().notifyChange(uri, null);
            return uri;
        }
    
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            Log.d(TAG, "delete");
            String table = getTableName(uri);
            if (table == null) {
                throw new IllegalArgumentException("Unsupported URI: " + uri);
            }
            int count = mDb.delete(table, selection, selectionArgs);
            if (count > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            return count;
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String selection,
                String[] selectionArgs) {
            Log.d(TAG, "update");
            String table = getTableName(uri);
            if (table == null) {
                throw new IllegalArgumentException("Unsupported URI: " + uri);
            }
            int row = mDb.update(table, values, selection, selectionArgs);
            if (row > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            return row;
        }
    
        private String getTableName(Uri uri) {
            String tableName = null;
            switch (sUriMatcher.match(uri)) {
            case BOOK_URI_CODE:
                tableName = DbOpenHelper.BOOK_TABLE_NAME;
                break;
            case USER_URI_CODE:
                tableName = DbOpenHelper.USER_TALBE_NAME;
                break;
                default:break;
            }
    
            return tableName;
        }
    }
    

    根据Binder的工作原理,我们知道ContentProvider的六个方法均运行在ContentProvider的进程中,除了onCreate由系统回调并运行在主线程里,其他五个方法均由外界回调并运行在Binder线程池中。

    AndroidManifest里注册ContentProvider

            <provider
                android:name=".provider.BookProvider"
                android:authorities="com.chong.chapter02.book.provider"
                android:permission="com.chong.PROVIDER"
                android:process=":provider" >
            </provider>
    

    android:authorities是ContentProvider的唯一标识,通过这个熟悉外部应用就可以访问我们的BookProvider。此外,外界应用如果想访问BookProvider,就必须声明"com.chong.PROVIDER"这个权限。

    ** 外部访问ContentProvider **

    public class ProviderActivity extends Activity {
        private static final String TAG = "ProviderActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_provider);
        
            Uri bookUri = Uri.parse("content://com.chong.chapter02.book.provider/book");
            ContentValues values = new ContentValues();
            values.put("_id", 6);
            values.put("name", "程序设计的艺术");
            getContentResolver().insert(bookUri, values);
            Cursor bookCursor = getContentResolver().query(bookUri, new String[]{"_id", "name"}, null, null, null);
            while (bookCursor.moveToNext()) {
                Book book = new Book();
                book.bookId = bookCursor.getInt(0);
                book.bookName = bookCursor.getString(1);
                Log.d(TAG, "query book:" + book.toString());
            }
            bookCursor.close();
    
            Uri userUri = Uri.parse("content://com.chong.chapter02.book.provider/user");
            Cursor userCursor = getContentResolver().query(userUri, new String[]{"_id", "name", "sex"}, null, null, null);
            while (userCursor.moveToNext()) {
                User user = new User();
                user.userId = userCursor.getInt(0);
                user.userName = userCursor.getString(1);
                user.isMale = userCursor.getInt(2) == 1;
                Log.d(TAG, "query user:" + user.toString());
            }
            userCursor.close();
        }
    }
    
    运行结果

    ContentProvider除了支持对数据源的增删查改这四个操作,还支持自定义调用,这个过程是通过ContentResolverCall方法和ContentProviderCall方法来完成的。

    使用Socket

    Socket也称为“套接字”,是网络通信中的概念,它分为流式套接字(TCP)和用户数据报套接字(UDP)两种。Socket本身可以支持传输任意字节流,这里为了简单,仅传输文本信息,很显然,这是一种IPC方式。

    Socket通信需要声明访问网络权限:

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    实现很简单,直接上代码

    public class TCPServerService extends Service {
    
        private boolean mIsServiceDestoryed = false;
        private String[] mDefinedMessages = new String[] {
                "你好啊,哈哈",
                "请问你叫什么名字呀?",
                "今天北京天气不错啊,shy",
                "你知道吗?我可是可以和多个人同时聊天的哦",
                "给你讲个笑话吧:据说爱笑的人运气不会太差,不知道真假。"
        };
    
        @Override
        public void onCreate() {
            new Thread(new TcpServer()).start();
            super.onCreate();
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onDestroy() {
            mIsServiceDestoryed = true;
            super.onDestroy();
        }
    
        private class TcpServer implements Runnable {
    
            @SuppressWarnings("resource")
            @Override
            public void run() {
                ServerSocket serverSocket = null;
                try {
                    serverSocket = new ServerSocket(8688);
                } catch (IOException e) {
                    System.err.println("establish tcp server failed, port:8688");
                    e.printStackTrace();
                    return;
                }
    
                while (!mIsServiceDestoryed) {
                    try {
                        // 接受客户端请求
                        final Socket client = serverSocket.accept();
                        System.out.println("accept");
                        new Thread() {
                            @Override
                            public void run() {
                                try {
                                    responseClient(client);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            };
                        }.start();
    
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        private void responseClient(Socket client) throws IOException {
            // 用于接收客户端消息
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));
            // 用于向客户端发送消息
            PrintWriter out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(client.getOutputStream())), true);
            out.println("欢迎来到聊天室!");
            while (!mIsServiceDestoryed) {
                String str = in.readLine();
                System.out.println("msg from client:" + str);
                if (str == null) {
                    break;
                }
                int i = new Random().nextInt(mDefinedMessages.length);
                String msg = mDefinedMessages[i];
                out.println(msg);
                System.out.println("send :" + msg);
            }
            System.out.println("client quit.");
            // 关闭流
            MyUtils.close(out);
            MyUtils.close(in);
            client.close();
        }
    
    }
    

    这里通过判断服务端输入流的返回值来确定客户端是否断开连接。当客户端断开连接后,服务端这边的输入流会返回null,这个时候我们就知道客户端退出了。

    客户端代码

    public class TCPClientActivity extends Activity implements OnClickListener {
    
        private static final int MESSAGE_RECEIVE_NEW_MSG = 1;
        private static final int MESSAGE_SOCKET_CONNECTED = 2;
    
        private Button mSendButton;
        private TextView mMessageTextView;
        private EditText mMessageEditText;
    
        private PrintWriter mPrintWriter;
        private Socket mClientSocket;
    
        @SuppressLint("HandlerLeak")
        private Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MESSAGE_RECEIVE_NEW_MSG: {
                    mMessageTextView.setText(mMessageTextView.getText()
                            + (String) msg.obj);
                    break;
                }
                case MESSAGE_SOCKET_CONNECTED: {
                    mSendButton.setEnabled(true);
                    break;
                }
                default:
                    break;
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_tcpclient);
            mMessageTextView = (TextView) findViewById(R.id.msg_container);
            mSendButton = (Button) findViewById(R.id.send);
            mSendButton.setOnClickListener(this);
            mMessageEditText = (EditText) findViewById(R.id.msg);
            Intent service = new Intent(this, TCPServerService.class);
            startService(service);
            new Thread() {
                @Override
                public void run() {
                    connectTCPServer();
                }
            }.start();
        }
    
        @Override
        protected void onDestroy() {
            if (mClientSocket != null) {
                try {
                    mClientSocket.shutdownInput();
                    mClientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            super.onDestroy();
        }
    
        @Override
        public void onClick(View v) {
            if (v == mSendButton) {
                final String msg = mMessageEditText.getText().toString();
                if (!TextUtils.isEmpty(msg) && mPrintWriter != null) {
                    mPrintWriter.println(msg);
                    mMessageEditText.setText("");
                    String time = formatDateTime(System.currentTimeMillis());
                    final String showedMsg = "self " + time + ":" + msg + "\n";
                    mMessageTextView.setText(mMessageTextView.getText() + showedMsg);
                }
            }
        }
    
        @SuppressLint("SimpleDateFormat")
        private String formatDateTime(long time) {
            return new SimpleDateFormat("(HH:mm:ss)").format(new Date(time));
        }
    
        private void connectTCPServer() {
            Socket socket = null;
            while (socket == null) {
                try {
                    socket = new Socket("localhost", 8688);
                    mClientSocket = socket;
                    mPrintWriter = new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(socket.getOutputStream())), true);
                    mHandler.sendEmptyMessage(MESSAGE_SOCKET_CONNECTED);
                    System.out.println("connect server success");
                } catch (IOException e) {
                    SystemClock.sleep(1000);
                    System.out.println("connect tcp server failed, retry...");
                }
            }
    
            try {
                // 接收服务器端的消息
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                while (!TCPClientActivity.this.isFinishing()) {
                    String msg = br.readLine();
                    System.out.println("receive :" + msg);
                    if (msg != null) {
                        String time = formatDateTime(System.currentTimeMillis());
                        final String showedMsg = "server " + time + ":" + msg
                                + "\n";
                        mHandler.obtainMessage(MESSAGE_RECEIVE_NEW_MSG, showedMsg)
                                .sendToTarget();
                    }
                }
                System.out.println("quit...");
                MyUtils.close(mPrintWriter);
                MyUtils.close(br);
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    实际上通过Socket不仅仅能实现进程间的通信,还可以实现设备间的通信,当然前提是这些设备之间的IP地址互相可见,这其中涉及许多复杂的概念。

    选用合适的IPC方式

    各种IPC方式的优缺点

    相关文章

      网友评论

        本文标题:Android IPC(三)选用合适的IPC方式

        本文链接:https://www.haomeiwen.com/subject/myelkftx.html