Android 从异步任务发送到活动的消息

Android 从异步任务发送到活动的消息,android,Android,我试图从嵌入在服务中的异步任务向我的主活动发送消息。基本上,异步任务必须在输入时阻塞,并且不能在主活动线程中运行(阻塞已从下面的示例代码中删除)。但是,当数据进来时,我需要将其发送到主活动。我发现下面发送的消息永远不会成功。如果答案是在异步任务中移动绑定,您将如何做到这一点?如果可能的话,指向示例代码将是一个很大的帮助 public class InputService2 extends Service { int bufferSize = 1024; Process process; Data

我试图从嵌入在服务中的异步任务向我的主活动发送消息。基本上,异步任务必须在输入时阻塞,并且不能在主活动线程中运行(阻塞已从下面的示例代码中删除)。但是,当数据进来时,我需要将其发送到主活动。我发现下面发送的消息永远不会成功。如果答案是在异步任务中移动绑定,您将如何做到这一点?如果可能的话,指向示例代码将是一个很大的帮助

public class InputService2 extends Service {
int bufferSize = 1024;
Process process;
DataInputStream os;
TextView inputView;
byte[] buffer = new byte[bufferSize];
private MyAsyncTask inputTask = null;
       public void onCreate(){
           inputTask = new  MyAsyncTask();
           inputTask.execute((Void[])null);

       }
       private class MyAsyncTask extends AsyncTask<Void,Void,Void> {

        int mValue = 0;
        static final int MSG_SET_VALUE = 3;
            protected void onProgressUpdate(Void progress){

            }

            protected void onPostExecute(Void result) {

            }

            protected Void doInBackground(Void... params) {

                int i = 0;


                try {
                    mValue = 0x23;
            Message message =     Message.obtain(null,MSG_SET_VALUE,mValue,0);
                    mMessenger.send(message);
                }
                catch (Exception e) {

                }


            }
        } 
       class IncomingHandler extends Handler {
            @Override
            public void handleMessage(Message msg) {
            }
        }
        final Messenger mMessenger = new Messenger(new IncomingHandler());
        public IBinder onBind(Intent intent) {
            return mMessenger.getBinder();
        } 

}

看起来您的示例基于上的javadoc参考,但是您忽略了使其工作的许多实现细节。您必须返回并实现该示例中引用的完整功能,才能使用该特定模式:请仔细注意IncomingHandler类中的REGISTER_CLIENT和UN_REGISTER_CLIENT实现部分,因为这些是确保消息可以从服务传输到客户端的位活动

class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, msg.arg1, duration);
        toast.show();
    }
}

boolean mBound;
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the object we can use to
        // interact with the service. We are communicating with the
        // service using a Messenger, so here we get a client-side
        // representation of that from the raw IBinder object.
        mService = new Messenger(service);
        mBound = true;
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        mService = null;
        mBound = false;
    }
};

protected void onStart() {
    super.onStart();
    // Bind to the service
    bindService(new Intent(this, InputService2.class), mConnection,
            Context.BIND_AUTO_CREATE);
}