我为 android 制作了一个简单的 RPC 机制应用程序,我遇到了一个问题,我无法从 RPC 类返回 UI 线程。
基本上我有 3 个类(ServerActivity、ServerView 和 ServiceImplementation),我创建了 3 个类,因为我使用 RPC 和 Protocol Buffer 进行绘图。
服务器活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_sv = new ServerView(this);
setContentView(_sv);
rpcConnectionFactory = SocketRpcConnectionFactories.createServerRpcConnectionFactory(SERVER_PORT);
int nThreadPool = 1;
server = new RpcServer(rpcConnectionFactory, Executors.newFixedThreadPool(nThreadPool), true);
server.registerBlockingService(Service.newReflectiveBlockingService(new ServiceImpl(myServiceHandler)));
server.run();
}
Handler myServiceHandler = new Handler() {
public void handleMessage(Message msg) {
Log.i("Handler", "Handler IN");
_sv.set(msg.what); /*To communicate with the view*/
super.handleMessage(msg);
}
};
服务实现:
public CanvasServiceImpl(Handler mActivity) {
backToUIThread = mActivity;
}
public Response drawCircle(RpcController controller, Circle1 request)
throws ServiceException {
android.os.Message message = new android.os.Message();
message.what = 1;
ImplHandler.sendMessage(message);
Response response = Response.newBuilder().setResult("drawCircle Success").build();
return response;
}
我无法访问我的 UI 线程。有人知道为什么吗?
谢谢,罗伯特