Android Bound Service (1), androidbound

Source: Internet
Author: User

Android Bound Service (1), androidbound

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

Preface
The main reason for re-learning this technology is that I didn't learn it well before. At that time, I always felt that my work could be active and I could just finish my work, it has greatly affected my technical level. Therefore, I have a lot of review experiences in this blog. I am fortunate enough to receive the guidance of a senior who pointed out that if I only want to learn but have no practical application, there will be fewer progress. Therefore, I 'd better read the source code and try more practices on my own, in this way, we should be able to make little progress after learning for 10 thousand hours, so we started learning/reviewing Bound Service.

Bound Service
「 Bound Service is the Server in the Client-Server Interface. An Activity can bind a Service through the Bound Service, send and receive messages, and even conduct inter-process communication .」 The Service Class is an Abstract Class, while the Bound Service implements the Service Class and implements the onBind () method before the Activity can communicate with the Service. Therefore, let's first bind an Activity to a Service from a simple example so that the Service can provide services for the Activity. At the same time, I have submitted the related code of the exercise to GitHub (https://github.com/shanwu/InterProcessCommPractice), with different branches representing different examples, so that you can use the branch Comparison Feature on Github () compare the differences between different methods.


Master Branch: In-process communication refers to the Extending the Binder class on the official website.

Ipc_messanger_example: Cross-process communication refers to the example of Using a Messenger on the official website (sorry for the English spelling of branch name Orz)

Ipc_messenger_example_bidirection: Same as above, but it is an example of mutual transmission of services and activities.

Ipc_aidl_example: Cross-process communication. A simple aidl example is used.


Extending Binder Service
The client Activity can establish a connection with the Service of the server by calling the bindService () method. When the connection is established, the onBind () method of the Bound Service is called first, and then onServiceConnected () is called (), if you need to remove the link, call the unbindService () method. In this case, the Bound Service directly calls onDestory () and the Service ends. The Code is as follows:
In the Bound Service, write a class that inherits the Binder-LocalBinder. It is mainly used to return the Bound Service object during onBind () during intra-process communication:

    public class LocalBinder extends Binder {        public Service getService() {            return WorkerService.this;        }    }
Declare a private global variable of LocalBinder
public class WorkerService extends Service {    private final IBinder mBinder = new LocalBinder();
Override 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 override the ServiceConnection method. At the beginning of the link, iBinder is the IBinder class variable returned by onBind, So we convert it into LocalBinder, then, call the method in LocalBinder to get the Bound Service object and place it in the global variable. Then, you can call its method. It is important to note that onServiceDisconnected is generally not called. It is called only when the link ends due to a Service exception.

        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 it to enable the Bound Service to do things. It can play music or execute other work that does not require the UI.
            case R.id.btn_sing:                if (mIsBind) {                    mService.makeSingingService();                }                break;


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

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.