Android Service full resolution (2)-bindService

Source: Internet
Author: User

Mainactivity is as follows:

Package cc. testservice2; import android. app. activity; import android. content. componentName; import android. content. intent; import android. content. serviceConnection; import android. OS. bundle; import android. OS. IBinder; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView;/*** Demo Description: * use the bindService () method to start the service * the local service described in this Demo, that is, the service and the visitor are in the same Process ** client summary: * core purpose of the client (MainActivity here): * Get the Binder object returned by the Service, then use this Binder object to call the related methods in the service ** detailed steps: * 1 Write A ServiceConnectionImpl class to implement the ServiceConnection interface * that is, private final class ServiceConnectionImpl implements ServiceConnection * 2 create a ServiceConnectionImpl class Object conn. then use Context to activate the service and establish a connection with the Service * that is, this. bindService (service, conn, this. BIND_AUTO_CREATE); * 3 overwrite the public void onServiceConnecte of the ServiceConnectionImpl class D (ComponentName name, IBinder service) * method. This is a callback Method !!!! After the client establishes a connection with the service, the public IBinder onBind (Intent intent) () method * of the server returns an IBinder object to the client. this IBinder object is the service * 4 in the * public void onServiceConnected (ComponentName, IBinder service) method. Use the IBinder object returned by the server to call the method in the service. ** note: * 1 The mServiceConnectionImpl object is used for binding and unbinding, and its class implements the ServiceConnection interface * 2 note that not only do you need to override the onCreate () of MainActivity to establish a connection with the service, and rewrite the onDestroy () method of the Activity! * So that the connection to the service is closed when the Activity exits. * 3. Why define an interface in this small application? * Because the service returns a Binder object to MainActivity, MainActivity receives this object. This object is an internal class object in the service. * In general, internal classes are private. Therefore, in MainActivity, it is impossible to add () An internal class object to receive this object * So an interface is defined here ---> to receive the Binder object using the interface. in this way, the business is abstracted as an interface. ** server Summary: * 1. The Custom Service ServiceSubclass inherits from the Service * 2. Write a custom internal class BinderSubclass to implement the business interface, and inherits from Binder * 3 to override the public IBinder onBind (Intent intent) method of the Service, and returns an IBinder object. * BinderSubclass class Object */public class MainActivity extends Activity {TextView mNumberTextView; TextView mResultTextView; Button mSearchButton; ServiceConnectionImpl callback; QueryInterface mBinder; public void onCreate (Bundle created) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // The Binder object mServiceConnectionImpl = new ServiceConnectionImpl (); mNumberTextView = (TextView) findViewById (R. id. numberEditText); mResultTextView = (TextView) findViewById (R. id. resultTextView); mSearchButton = (Button) findViewById (R. id. searchButton); mSearchButton. setOnClickListener (new ButtonOnClickListener (); Intent intent = new Intent (this, ServiceSubclass. class); // The bindService (intent, mServiceConnectionImpl, this. BIND_AUTO_CREATE);} private class ButtonOnClickListener implements OnClickListener {public void onClick (View v) {String number = mNumberTextView. getText (). toString (); String result = mBinder. queryByNumber (Integer. valueOf (number); mResultTextView. setText (result) ;}}// when binding a service and unbinding a service private final class ServiceConnectionImpl implements ServiceConnection {// when binding a service, this method calls public void onServiceConnected (ComponentName, IBinder service) {mBinder = (QueryInterface) service;} // when the service is removed, this method calls public void onServiceDisconnected (ComponentName name) {mBinder = null ;}} // remove the connection from the service. protected void onDestroy () {unbindService (mServiceConnectionImpl); super. onDestroy ();}}

Servicesubclass:

Package CC. testservice2; import android. app. service; import android. content. intent; import android. OS. binder; import android. OS. ibinder;/*** server Summary: * 1. The Custom Service servicesubclass inherits from the Service * 2. Write a custom internal class bindersubclass to implement the business interface, and inherits from binder * 3 to override the public ibinder onbind (intent) method of the service, and returns an ibinder object. * bindersubclass Class Object */public class servicesubclass extends Service {private string [] names = new string [] { "Xiao Ming", "Xiao Wang", "Xiao Yang", "Xiao Li", "Xiao Qiang"}; bindersubclass mbindersubclass = new bindersubclass (); @ overridepublic ibinder onbind (intent) {return mbindersubclass;} // querybynumber is the Business Method in the interface. // In general, the service is abstracted as an interface and then implemented, for example, here. // Note: bindersubclass inherited from binder also implements the business interface private final class bindersubclass extends binder implements QueryInterface {Public String querybynumber (INT number) {return query (number );}} // Public String query (int I) {if (I> 0 & I <6) {return Names [I-1];} return "query error, enter ";}} again ";}}

QueryInterface:

Package cc. testservice2; // service interface public interface QueryInterface {public String queryByNumber (int number );}

Main. xml is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "enter a number ranging from 1 to 5"/> <EditText android: id = "@ + id/numberEditText" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> <Button android: id = "@ + id/searchButton" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "query"/> <TextView android: id = "@ + id/resultTextView" android: layout_width = "fill_parent" android: layout_height = "wrap_content"/> </LinearLayout>

 

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.