Android Bound Service (i)-----extending Binder Service (a simple example of in-process binding service)

Source: Internet
Author: User

Ref:http://developer.android.com/guide/components/bound-services.html?

Objective
Re-learning this technology, the main reason, is because there was no good learning, then always feel that the work is active, can complete the work is good, and this muddle-through idea, greatly affected my technical level, and therefore, in this blog, there are many review experience. Fortunate to get a senior guidance, pointed out, if only learning, and no practical application, so progress will be less, therefore, it is best to look at the source code, and try more practice, so that learning 10,000 hours, should be able to have small progress, so began the Bound service related study/review work.

Bound Service
"bound service is the server in Client-server Interface, an Activity can bind a service through the Bound service, send and receive messages, and even communicate interprocess. The "service class is an Abstract class, and the Bound service implements the service class and implements the Onbind () method, in which Activity can communicate with the service. So, let's start with a simple example--to have an activity bind a service, to let the services serve the Activity, and I've submitted the code for the exercise to GitHub (Https://github.com/shanwu /interprocesscommpractice), there are different branches representing different examples, so you can compare the differences between different methods using the branch comparison function () on GitHub.


Master Branch : In-process communication, represented by the official website extending the Binder class example.

ipc_messanger_example: cross-process communication, representative of the official website Using A messenger example (sorry branch name English misspelled Orz)

ipc_messenger_example_bidirection: ibid., but it is a Service, Activity two-way mutual transmission example.

ipc_aidl_example: cross-process communication, simple aidl use example.


Extending Binder Service
The client's Activity can be connected through the call Bindservice () method and service-side services, when the link is established, the Onbind () method of the Bound service is called first, and then the onserviceconnected () is called. , and if you need to remove the link, then call the Unbindservice () method, at which point the bound service calls Ondestory () directly, and the services end. The code is as follows:
The Bound service writes a class-localbinder that inherits binders, primarily during in-process communication, when Onbind () is able to return the object of the Bound service with a method:

    public class Localbinder extends Binder {public        Service GetService () {            return workerservice.this;        }    }
Declare a localbinder private global variable
public class Workerservice extends Service {    private final ibinder Mbinder = new Localbinder ();
Overwrite the Onbind () method at the same time
    @Override public    IBinder onbind (Intent Intent) {        log.d ("Guang", "Onbind");        return mbinder;    }


On the activity side, we need to use Bindservice () to bind the Bound service.

    @Override public    void OnClick (View v) {        final int id = V.getid ();        Switch (ID) {case            R.id.btn_start:                Intent Intent = new Intent (mainactivity.this, workerservice.class);                Bindservice (Intent, Mserviceconn, context.bind_auto_create);                Break

At the same time we need to overwrite the method of interface Serviceconnection, at the beginning of the link, IBinder is the IBinder class variable returned by Onbind, so we convert it to Localbinder, Then call the method in Localbinder to get the object of the Bound Service, put it in the global variable, and then call his method. The note here is that onservicedisconnected will not normally be called, only if the service has an exception that causes the link to end.

        Mserviceconn = new Serviceconnection () {            @Override public            void onserviceconnected (ComponentName componentname , IBinder ibinder) {                Workerservice.localbinder Lbinder = (workerservice.localbinder) ibinder;                Mservice = (workerservice) lbinder.getservice ();                LOG.D ("Guang", "onserviceconnected");                Misbind = true;            }            @Override public            void onservicedisconnected (ComponentName componentname) {                log.d ("Guang", " Onservicedisconnected ");                Misbind = false;            }        };
Call its method to make the Bound Service work, it can play music, or perform other tasks that do not require a UI interface.
            Case r.id.btn_sing:                if (misbind) {                    mservice.makesingingservice ();                }                Break


This is basically the focus of this article ~ All code can be obtained from GitHub ~

Android Bound Service (i)-----extending Binder Service (a simple example of in-process binding service)

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.