Android Services implement inter-thread communication through Messenger

Source: Internet
Author: User

In fact, there are not many Messenger actually used in actual use, but the examiner still wants to examine your basic skills during the interview. Now let's take a look at the use cases of Messenger. If you need your Service to communicate with a remote thread, you can use a Messenger interface for your Service. This technology allows you to perform inter-process communication (IPC) without using AIDL. What are the basic conclusions about the use of Messenger. (1) In Service, a Handler class must be inherited to process callback Information (2) the parameters passed during the creation of the Messenger class are the Handler class we mentioned earlier (3) return Messenger in the onbind () method of the Service. each message received by the getBind () method (4) Service is processed by the handlerMessage () method. Let's take a look at the code.

MainActivity code

Package com. example. f24_service03; import android. OS. bundle; import android. OS. IBinder; import android. OS. message; import android. OS. messenger; import android. OS. remoteException; import android. app. activity; import android. content. componentName; import android. content. intent; import android. content. serviceConnection; import android. view. view; public class MainActivity extends Activity {private boolean flag = False; private Messenger messenger; public void sayHello (View v) {if (! Flag) return; // Create and send a message to the service, using a supported 'wh' // valueMessage msg = Message. obtain (null, 1, 0, 0); try {messenger. send (msg);} catch (RemoteException e) {e. printStackTrace () ;}} ServiceConnection connection = new ServiceConnection () {@ Overridepublic void onServiceDisconnected (ComponentName name) {// TODO Auto-generated method stubmessenger = null; flag = false ;} @ Overridepublic void onServiceConnected (ComponentName name, IBinder service) {// TODO Auto-generated method stub // when the binding is successful, we can use Messenger to implement inter-thread communication. messenger = new Messenger (service); flag = true ;};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} protected void onStart () {Intent intent = new Intent (this, MyService. class); bindService (intent, connection, BIND_AUTO_CREATE); super. onStop () ;}; @ Overrideprotected void onStop () {// TODO Auto-generated method stubif (flag) {unbindService (connection); flag = false;} super. onStop ();}}

MyService code

Package com. example. f24_service03; import android. annotation. suppressLint; import android. app. service; import android. content. intent; import android. OS. handler; import android. OS. IBinder; import android. OS. message; import android. OS. messenger; import android. util. log; import android. widget. toast; public class MyService extends Service {@ SuppressLint ("HandlerLeak") // This class processes the information passed in from the client class incomingHandler extends Handler {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubif (msg. what = 1) {Toast. makeText (getApplicationContext (), "Hello, already recieve Message", Toast. LENGTH_LONG ). show ();} super. handleMessage (msg) ;}} final Messenger messenger = new Messenger (new incomingHandler (); // when a Service is bound, we will return a Messenger Communication Interface @ Overridepublic IBinder onBind (Intent intent) {// TODO Auto-generated method stubLog. I ("Service", "--------> bind"); return messenger. getBinder ();}}


Related Article

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.