Four major Android components Learning Service 6

Source: Internet
Author: User

Four major Android components Learning Service 6

In the previous sections, we learned how to use StartServer to start a service, bindServer to bind a service, and the service lifecycle, and what is IntentService.

Some readers may find that the second parameter in BindServer is ServiceConnection, which seems useless when bindService is used. In this lesson, we will learn.

 

Let's talk about it first. When I went to college, I was not good at learning, but I was still the kind of students who often missed classes. After a long period of time, the teacher had a hard time mixing up. I tried to compete when I selected a member in the class. However, because I often don't attend classes, my chances are low, so I thought of taking a backdoor. Why do you give the guide a little money? The Anti-director is corrupt and corrupt. But I don't know the facilitator. What should I do? But my roommates are the red ones before the guide. You can use my roommates to send money to the Guide and then ask the Guide to give it to my entire party member.

The general process is as follows:

Then we convert this into code:
 

This is equivalent to the method we use to start a service for an Activity, but the service has a method. The Activity can only be used by a third party but cannot be used.

"Activity" is "me", and "Seriver" is "corrupt official guide". Only the Guide can give you a place to become a Party member, but you don't know the guide, so you have to find your roommate, that is, the cronies of the Facilitator.

 

Now the question is: I want to contact my roommates, that is, the cronies of the Guide.

Which of the two ways to start the service, which kind of service can return this kiss to you after it is created? Obviously, the services started by startService do not exist. However, the bindService startup service includes:

@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}

After the bindService starts the service, the return value is returned. The default value is null.

 

After analysis, the bindService can be returned to the service. The burden is not the service itself, but the service proxy.

 

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 (); // bindService (intent, conn, BIND_AUTO_CREATE);} // public void toDoDangyuan (View v) {// call the roommate's method me. toBeDangYuan ();} class MyServiceConn implements ServiceConnection {// call @ Overridepublic void onServiceConnected (ComponentName name, IBinder service) after the service connection is successful) {// obtain the 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 the proxy of the Service, that is, return new SheYou ();} // exclusive class SheYou extends Binder {// exclusive class SheYou in the middle (SheYou) method for roommates to make you a member public void ToBeDangYuan () {DangYuan () ;}// service method, that is, the Authority public void DangYuan () {Log. I (DangYuanService, you only need 1000 yuan to become a Member !);}}

The running effect will be known: my roommates found the guide and the guide said, "Only 1000 yuan is required ". Look, there's a lot of greed among the guides.

 

I wanted to report to corrupt officials, but I still wanted to graduate. No corrupt officials were reported.

 

Next, our guide has another hobby, that is, drinking. Of course, my roommates must attend.

 

Class SheYou extends Binder {// exclusive method in the middle (roommates), making you a party member public void ToBeDangYuan () {DangYuan ();} public void HeJiu () {Log. I (DangYuanService, accompanied by guides for drinking !!!!);}}

Add a new method to my roommates for drinking with guides

 

Of course, if you drink with the guide, you can only participate with the Guide's cronies. People like me cannot participate. Of course, you do not have the right to know.

 

// Public void toDoDangyuan (View v) {// call me. ToBeDangYuan (); me. HeJiu ();}
Now, the method for drinking is OK. I do not have the right to know the truth. That won't work. Modify the service

 

At this time, we will create an excuse dedicated to serving as a Party member:

Public interface DangYuanInterface {// there is only one method, you can know, that is, the Party member's affairs public void ToBeDangYuan ();}
Then let the roommates, that is, the intermediary, implement this interface.

 

 

Class SheYou extends Binder implements DangYuanInterface {// exclusive method in the middle (roommates), making you a party member public void ToBeDangYuan () {DangYuan ();} public void HeJiu () {Log. I (DangYuanService, accompanied by guides for drinking !!!!);}}
Then, the Activity defines the interface of a Party member.

 

 

DangYuanInterface dangyuan;

Then obtain the man-in-the-middle object:

 

 

Public void toDoDangyuan (View v) {// call the roommate's method dangyuan. toBeDangYuan ();} class MyServiceConn implements ServiceConnection {// call @ Overridepublic void onServiceConnected (ComponentName name, IBinder service) after the service connection is successful) {// obtain the service proxy dangyuan = (DangYuanInterface) service;} @ Overridepublic void onServiceDisconnected (ComponentName name) {// TODO Auto-generated method stub }}

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


In this way, if you want to obtain the methods in the service, you can get the Service proxy through the onBind return value. Remember that it is not the service itself. Generally, the proxy or intermediary of the service is used to extract the method unique to the intermediary into an interface.

 

 

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.