`

Android 远程回调

阅读更多

1.点击button 发送Handler1 Message  收到消息  开启线程进行处理

2.收到消息 后调用已经绑定的 mService   mService.sendMail()方法

  mService.sendMailAlarm(SEND_MAIL_TYPE,subject,body.toString()); --Activity类

 

 

Service类

  private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {

 

 public void sendMail(int type, String subjectText, String sendText)
                throws RemoteException {
            Constant.Log("sendMailAlarm", "RunKKKKKK");
            new SendMailAlarmMsgThread(type, subjectText, sendText).start();
        }



 

3 。SendMailAlarmMsgThread  处理完发送 Service Handler2 消息

 

4. 处理接受到的信息后 回调函数调用

Service类

private final RemoteCallbackList<IRemoteServiceCallback> mCallbacks = new RemoteCallbackList<IRemoteServiceCallback>();

 

private void _makeSendMailResultCallbacksWork(int result) {
        Constant.Log("RemoteService", "_makeSendMailResultCallbacksWork");
        // Broadcast to all clients the new value.
        Constant.Log("onSendMailReturn", "RunKKKKKK");
        try {
            final int N = mCallbacks.beginBroadcast();
            for (int i = 0; i < N; i++) {
                try {
                    mCallbacks.getBroadcastItem(i).onSendMailReturn (result);
                } catch (RemoteException e) {
                    Constant.exception(e);
                }
            }
            mCallbacks.finishBroadcast();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

onSendMailReturn  

在Activity 类中实现

 

 

  private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {

     
        @Override
        public void onSetPowerStatusReturn(String arg0, String arg1) throws RemoteException {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSendMailReturn(int arg0) throws RemoteException {
            // TODO Auto-generated method stub
            LogOut("call method : onSendMailReturn()---" + arg0);
            // if (!isClickMailBtn) return ;
            // isClickMailBtn = false ;
            switch (arg0) {
                case 0:
                    mHandler.removeMessages(MAIL_RECEIVE_SUCCESS_RESULT_ORDER);
                    mHandler.sendEmptyMessage(MAIL_RECEIVE_SUCCESS_RESULT_ORDER);

                    break;
                case 1:
                    mHandler.removeMessages(MAIL_RECEIVE_FAILURE_RESULT_ORDER);
                    mHandler.sendEmptyMessage(MAIL_RECEIVE_FAILURE_RESULT_ORDER);
                    break;
                default:
                    break;
            }
        }
    };

 处理完后发送Handler1 消息 更新画面

 

 

耗时操作UI更行超过5秒!

 

 

Service  参考http://hi.baidu.com/xtlp/blog/item/e6e18810ad6c030c213f2eaf.html 

本地服务和远程服务

 

 

Android 监听事件 一旦监听一直会有守护线程监听。

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics