ps:阅读本文 需要对android 多进程编程有一定了解。

1.Android中总共有几种方式进行IPC?

答:一共有两种,一种是binder 还有一种是socket。Binder 大家用的比较多。Socket很少有人用,这里给出一个利用Socket进行ipc通信的例子。

服务端代码:

 1 package com.example.administrator.socketipcdemo;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.IBinder;
 6 import android.util.Log;
 7 
 8 import java.io.BufferedReader;
 9 import java.io.BufferedWriter;
10 import java.io.IOException;
11 import java.io.InputStreamReader;
12 import java.io.OutputStreamWriter;
13 import java.io.PrintWriter;
14 import java.net.ServerSocket;
15 import java.net.Socket;
16 
17 public class ServerService extends Service {
18 
19     public static final int SERVER_PORT_NUMBER = 5678;
20     private boolean mServiceDestroyed = false;
21 
22     @Override
23     public void onCreate() {
24         new Thread(new ServerRunnable()).start();
25         super.onCreate();
26     }
27 
28     @Override
29     public void onDestroy() {
30         mServiceDestroyed = true;
31         super.onDestroy();
32     }
33 
34     public ServerService() {
35     }
36 
37     @Override
38     public IBinder onBind(Intent intent) {
39         // TODO: Return the communication channel to the service.
40         throw new UnsupportedOperationException("Not yet implemented");
41     }
42 
43     //这个线程 用于监听端口号
44     class ServerRunnable implements Runnable {
45         @Override
46         public void run() {
47 
48             ServerSocket serverSocket = null;
49             try {
50                 serverSocket = new ServerSocket(SERVER_PORT_NUMBER);
51             } catch (IOException e) {
52                 e.printStackTrace();
53             }
54             while (!mServiceDestroyed) {
55                 try {
56                     final Socket client = serverSocket.accept();
57                     new Thread() {
58                         @Override
59                         public void run() {
60                             try {
61                                 responseClientRequest(client);
62                             } catch (IOException e) {
63                                 e.printStackTrace();
64                             }
65                         }
66                     }.start();
67 
68                 } catch (IOException e) {
69                     e.printStackTrace();
70                 }
71             }
72         }
73     }
74 
75     private void responseClientRequest(Socket client) throws IOException {
76         BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
77         PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
78         while (!mServiceDestroyed) {
79             String str = in.readLine();
80             if (str == null) {
81                 break;
82             }
83             String reponseStr = "我是服务器端,我收到了来自客户端的消息:" + str;
84             out.println(reponseStr);
85         }
86         out.close();
87         in.close();
88         client.close();
89     }
90 }
View Code

相关文章:

  • 2021-08-12
  • 2021-08-19
  • 2022-01-16
  • 2022-12-23
  • 2022-01-13
  • 2021-12-11
猜你喜欢
  • 2022-12-23
  • 2022-01-19
  • 2021-07-15
  • 2021-11-05
  • 2022-12-23
  • 2021-06-13
相关资源
相似解决方案