Android Service Communication

Source: Internet
Author: User

One, Local service communication

LocalService

/** * Created by LSJ on 2015/8/29. * This is a LocalService */public class Strreplaceservice extends Service {    private final String tag= "Strreplaceservice"; C1/>public class Mybinder extends binder{public        strreplaceservice GetService () {            return strreplaceservice.this;        }    }    @Override public    void OnCreate () {        super.oncreate ();        LOG.D (TAG, "onCreate");    }    @Override public    IBinder onbind (Intent Intent) {        log.d (TAG, "Onbind");        return  new Mybinder ();    }    @Override Public    Boolean onunbind (Intent Intent) {        log.d (TAG, "Onunbind");        return Super.onunbind (intent);    }    @Override public    void OnDestroy () {        log.d (TAG, "OnDestroy");        Super.ondestroy ();    }    Public String Strreplace () {        string result = Stringreplace.test ();        LOG.D (Tag,result);        return result;}    }

Test activity

public class Stringreplaceactivity extends Baseactivity {private TextView Resulttv;    Private Serviceconnection SC;    Private Strreplaceservice Strreplaceservice;    String result = "string";        @Override public void OnClick (View v) {} @Override protected void OnStart () {Super.onstart ();        Intent Intent = new Intent (this, strreplaceservice.class);    OnCreate time already had SC Bindservice (intent, SC, context.bind_auto_create);        } @Override protected void OnStop () {super.onstop ();    Unbindservice (SC);    } @Override protected void Findview () {Setcontentview (r.layout.activity_str_replace);        } @Override protected void Initview () {Resulttv = (TextView) Findviewbyid (R.ID.STR_REPLACE_RESULTTV);  sc = new Serviceconnection () {@Override public void onserviceconnected (componentname name, IBinder Service) {Strreplaceservice = ((strreplaceservice.mybinder) service). GetService ();            This can be called Doservice () in the onclick;    } @Override public void onservicedisconnected (componentname name) {}}; }/** * Call method in Strreplaceservice */private void Doservice () {result = Strreplaceservice.strreplace ()        ;        LOG.D (TAG, result);    Resulttv.settext (result);   } @Override protected void Setonclicklistener () {}}

  

Second, cross-process service communication

Remoteservice

/** * This is remote service */public class Cqueueservice extends service {private final String TAG = "Cqueueservice";    public static final int msg_no = 0; Class Incominghandler extends handler{@Override public void Handlemessage (Message msg) {if (ms                G.replyto! = null) {//receives a message and sends a response message to the client msg_to_client = This.obtainmessage ();                Msg_to_client.what = cqueueactivity.msg_no_to_client;                    try {//Get Messenger from the client and send it a message Cmessenger = Msg.replyto;                Cmessenger.send (msg_to_client);                    }catch (RemoteException e) {}} switch (msg.what) {case MSG_NO:                    Toast.maketext (Cqueueservice.this, "msg from client", Toast.length_long). Show ();                Break            Default:super.handleMessage (msg); }        }    }    /**     * Own Messenger */messenger smessenger= new Messenger (New Incominghandler ());    The client Messenger Messenger Cmessenger;        Public Cqueueservice () {} @Override public void OnCreate () {LOG.E (TAG, "onCreate");    Super.oncreate ();        } @Override Public IBinder onbind (Intent Intent) {log.e (TAG, "Onbind");        Returns the binder of its own messenger, which is used by the client to obtain the service's Messenger object through Binder (new Messenger (binder)).    return Smessenger.getbinder ();        } @Override public boolean onunbind (Intent Intent) {log.e (TAG, "Onunbind");    return Super.onunbind (Intent); }}

In the configuration file

<!--remote service        -to <service            android:name= ". Service. Cqueueservice "            android:enabled=" true "            android:exported=" true "            android:process=": Remote >        </service>

Test activity

public class Cqueueactivity extends baseactivity{public static final int msg_no_to_client=0;    Serviceconnection MSc; /** * Handler of incoming MSG from SERIVCE */class Incominghandler extends handler{@Override PU                    Blic void Handlemessage (Message msg) {switch (msg.what) {case msg_no_to_client:                    Toast.maketext (Cqueueactivity.this, "ack_msg from service", Toast.length_long). Show ();                Break            Default:super.handleMessage (msg);    }        }    } ;    /** * Own Messenger */messenger Mmessenger = new Messenger (new Incominghandler ());    Remote Service Messenger Messenger Smessenger;    @Override protected void Findview () {Setcontentview (r.layout.activity_cqueue); } @Override protected void Initview () {mSc = new serviceconnection () {@Override publi c void onserviceconnected (ComponentName naMe, IBinder service) {LOG.D (TAG, "onserviceconnected");                Get Messenger Smessenger = new Messenger (service) on the server;            Send a message to the server sendmsg (); } @Override public void onservicedisconnected (componentname name) {LOG.D (TAG, "Onser            Vicedisconnected ");    }        } ;        /** * Sends a message to it using the server's Messenger */private void sendmsg () {Message Msg_to_service = new Message ();        Msg_to_service.replyto = Mmessenger;        try{smessenger.send (Msg_to_service);        }catch (RemoteException e) {e.printstacktrace ();        }} @Override protected void OnStop () {super.onstop ();    This.unbindservice (MSC);        } @Override protected void OnStart () {Super.onstart ();        LOG.D (TAG, This.getapplicationcontext (). Getpackagecodepath ());     Intent service = new Intent (this, cqueueservice.class);   This.bindservice (service, MSc, context.bind_auto_create); } @Override public void OnClick (View v) {} @Override protected void Setonclicklistener () {}}

  

  

Android Service Communication

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.