Android Four component Learning Service six

Source: Internet
Author: User

The last few sections. We learn how to start a service with StartServer and use Bindserver to bind a service. And the life cycle of the service, and what is Intentservice.

Perhaps some readers will find that the second parameter in our bindserver is serviceconnection, which seems useless when using Bindservice.

That's the lesson to learn.


Let's talk first. When I was in college. I am not good at learning, or the kind of classmate who often absenteeism.

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 of the 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. Then let the pilot give me the 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, the activity wants to use but cannot get, only can through the third party to help him to take.

The activity is me, and Seriver is the official officer, only a 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 a trusted member of the guide."

The two ways to start the service. What kind of service can be returned to your cronies when it's created? Obviously StartService started the service is not.

However, the Bindservice start-up 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 is able to return to the service, is not the service itself, just 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) {//Returns the agent for the service. 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!");}}

The implementation of the effect will know: My roommate to find the guide, the guide said, "Just 1000 yuan Ji-line." See, the lead is greedy.


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


Then we have a hobby for our guide. That is drinking, of course, my roommate will need to participate.

Class Sheyou extends binder{//in the middle (roommate) unique method. Make you a 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 is only the guidance of the cronies of the ability to participate. People like me at a level. That's not going to add up. 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 ();        }
Now I'm still on the right way to ask for a drink.

I have no right to know, according to the truth.

That's not going to be. So, the service changes.

At this point we create a special excuse to be a party member only:

Public interface Dangyuaninterface {//There is only one way you can know. That's the party thing. public void Tobedangyuan ();}
Then let the roommate. That's the middleman, to 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 of the matter. You have no right to ask other things.


In this way, you need to get the methods in the service. will be able to return the service through the onbind of the proxy. Remember that it is not the service itself.

In general, the agent or intermediary of the service, generally the middleman unique method of the interface.


Android Four component 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.