Android Four Components Learning service six

Source: Internet
Author: User

In the last few sections, we learned how to start a service with StartServer, bind a service with Bindserver, and the life cycle of a service, and what is Intentservice.

Perhaps some readers will find that the second parameter in our bindserver is serviceconnection, which seems to be useless when using Bindservice. That's the lesson to learn.


Let's say something first, when I was in college, I didn't study well, or the kind of classmate who often skipped school. In the long run, the teacher in front of the mixed bad. There is a class in the selection of party members, I want to compete, but because I often do not class students, the natural odds are small, so I think back door. I give the guide to the money, anyway the guide is corrupt officials, is corrupt officials which have not greedy. But I don't know the pilot. But my roommate that is the guide before the Reds, can through my roommate to give money to the guide, and then let the guide to my whole party.

The approximate process is this:

So let's turn this into code:

This is quite similar to what we use for an activity to start a service, but the service has a method that the activity wants to use but is not able to get, only through a third party to help him.

Activity is me, and Seriver is the official officer, only the guide can give you a place to make you a party member, but you do not know the guide, so you have to find your roommate, that is, the guide's cronies.


The question now goes: "I want to find my roommate, who is the guide's confidant."

What are the two ways to start a service that can be returned to your cronies when it is created? Obviously StartService started the service is not. However, the Bindservice startup service is:

@Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn null;}

That is, Bindservice starts the service, returns the value by return, which is null by default.

Since the analysis, Bindservice can be returned to the service, not the service itself, but only the agent of the service.

public class Mainactivity extends Activity {private Intent intent;private Myserviceconn Conn; Sheyou me;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);                Intent = new Intent (this, dangyuanservice.class);        conn = new Myserviceconn ();        Bind service        Bindservice (Intent, Conn, bind_auto_create);    }        I want to be a party member of the method public    void Tododangyuan (View v)    {    //Call the roommate's method    me. Tobedangyuan ();    }        Class Myserviceconn implements Serviceconnection    {    //@overridepublic void onserviceconnected is called when the service connection is successful ( ComponentName name, IBinder service) {//Get service Proxy me = (sheyou) service;} @Overridepublic void onservicedisconnected (componentname name) {//TODO auto-generated Method stub}}}    

Service Code:
public class Dangyuanservice extends service {@Overridepublic IBinder onbind (Intent Intent) {//Return to the service agent, that is, the official officer of the corrupt officials return New Sheyou ();} The only way to give me a party member is the class Sheyou extends binder{//in the middle (roommates) unique method, so that you become a member of public void Tobedangyuan () {Dangyuan ();}} Service your own method, that is, the right of the Guide public void Dangyuan () {log.i ("Dangyuanservice", "only 1000 yuan can become party members!");}}

Run the effect will know: My roommate to find the guide, the guide said "only 1000 yuan Ji-line." See, the lead is greedy.


I wanted to report the corrupt officials at the time, but I was thinking about graduating. Did not report corrupt officials.


Then, our guide has a hobby, that is, drinking, of course, my roommate must attend.

Class Sheyou extends binder{//in the middle (roommates) unique method, let you become party member public void Tobedangyuan () {Dangyuan ();} public void Hejiu () {log.i ("Dangyuanservice", "accompany the pilot to drink!!!!");}}

Add a new way to my roommate, drink with the pilot.

Of course, accompany the guide to drink, that only the guide's cronies can participate, such as my level of people, it is not able to participate. Of course, I don't know.

    I want to be a party member of the method public    void Tododangyuan (View v)    {    //Call the roommate's method    me. Tobedangyuan ();    Me. Hejiu ();        }
And now I'm still OK with the way to drink. I have no right to know, according to the truth. That's not going to be. Then modify the service

This is when we create an excuse for only being a party member:

Public interface Dangyuaninterface {///There is only one way, you can know, that is the matter of the party member public void Tobedangyuan ();}
Then let the roommate, the middleman, implement this interface

Class Sheyou extends Binder implements dangyuaninterface{//in the middle (roommates) unique method that allows you to become a member of public void Tobedangyuan () {Dangyuan ();} public void Hejiu () {log.i ("Dangyuanservice", "accompany the pilot to drink!!!!");}}
Then, the activity defines the interface of a party member

Dangyuaninterface Dangyuan;

Then get this Middleman object:

    public void Tododangyuan (View v)    {    //Call roommate method    Dangyuan. Tobedangyuan ();        }        Class Myserviceconn implements Serviceconnection    {    //@overridepublic void onserviceconnected is called when the service connection is successful ( ComponentName name, IBinder service) {//the agent Dangyuan = (dangyuaninterface) service that gets services;} @Overridepublic void onservicedisconnected (componentname name) {//TODO auto-generated Method stub}    }

At this time you can only know about the party members, other things you have no right to ask.


In this way, you want to get the method in the service, you can return to the service by Onbind the proxy, remember not the service itself. In general, the agent or intermediary of the service, generally the middleman unique method of the interface.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Four Components Learning service six

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.