Android and androidsdk

Source: Internet
Author: User

Android and androidsdk

If you need your service to communicate with other processes, you can useMessengerTo provide this interface.

This method allows you to implement cross-process communication IPC without using AIDL.

Steps

The following is a summary of how to use Messenger:

1. The service implements a Handler to receive the callback of each call from the client.

2. Handler is used to create a Messenger object, which is a reference of Handler.

3. Create an IBinder for Messenger, and the service returns it to the client from onBind.

4. The client uses this IBinder to instantiate Messenger (service Handler reference), and the client uses it to send a Message object to the service.

5. The service receives each Message object in its Handler, in its handleMessage () method.

Code
public class MessengerService extends Service{    /** Command to the service to display a message */    static final int MSG_SAY_HELLO = 1;    /**     * Handler of incoming messages from clients.     */    class IncomingHandler extends Handler    {        @Override        public void handleMessage(Message msg)        {            switch (msg.what)            {                case MSG_SAY_HELLO:                    Toast.makeText(getApplicationContext(), "hello!",                            Toast.LENGTH_SHORT).show();                    break;                default:                    super.handleMessage(msg);            }        }    }    /**     * Target we publish for clients to send messages to IncomingHandler.     */    final Messenger mMessenger = new Messenger(new IncomingHandler());    /**     * When binding to the service, we return an interface to our messenger for     * sending messages to the service.     */    @Override    public IBinder onBind(Intent intent)    {        Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT)                .show();        return mMessenger.getBinder();    }}

Note that the handleMessage () method in Handler is used by the service to receive the incoming Message and decide what to do.

The client only needs to create a Messenger Based on the IBinder returned by the service, and then send the information using the send () method.

For example, a simple activity is bound to the service and sent to the service:

public class ActivityMessenger extends Activity{    /** Messenger for communicating with the service. */    Messenger mService = null;    /** Flag indicating whether we have called bind on the service. */    boolean mBound;    /**     * Class for interacting with the main interface of the service.     */    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;        }    };    public void sayHello(View v)    {        if (!mBound)            return;        // Create and send a message to the service, using a supported 'what'        // value        Message msg = Message                .obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);        try        {            mService.send(msg);        }        catch (RemoteException e)        {            e.printStackTrace();        }    }    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }    @Override    protected void onStart()    {        super.onStart();        // Bind to the service        bindService(new Intent(this, MessengerService.class), mConnection,                Context.BIND_AUTO_CREATE);    }    @Override    protected void onStop()    {        super.onStop();        // Unbind from the service        if (mBound)        {            unbindService(mConnection);            mBound = false;        }    }}

Note that this example does not show how the service responds to the client. If you want the service to respond, you need to create a Messenger in the client.

Then, when the client receives the onServiceConnected () callback method, it will send a Message to the service, which contains the client's Messenger in the replyTo parameter of its send () method.

I am the dividing line of tiantiao

 

 

Reference: http://www.cnblogs.com/mengdd/tag/Android/default.html? Page = 4


Android: id

Android: id = "@ + id/layout01": The layout01 variable is defined and is automatically written into R. java, in R. the internal class id is generated in the java file, which can be called in the main program. id. layout01 to obtain the layout variable entity.
Android: orientation = "vertical": the vertical layout. Horizontal for horizontal layout

Android: id

Android: id = "@ + id/layout01": The layout01 variable is defined and is automatically written into R. java, in R. the internal class id is generated in the java file, which can be called in the main program. id. layout01 to obtain the layout variable entity.
Android: orientation = "vertical": the vertical layout. Horizontal for horizontal layout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.