Android Local Service

Source: Internet
Author: User
// The service is in the same process as the visitor, so it is called the local service // Visitor: package cn.com. localquery; import cn.com. service. ILocalQuery; import cn.com. service. localQueryService; 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; // visitor (client) Summary: // core objective of the client (MainActivity here): To get the Binder object returned by the service, then use this Binder object to call the related methods in the service // steps: // 1 Write A LocalQueryServiceConnection class to implement the ServiceConnection interface // LocalQueryServiceConnection implements ServiceConnection // 2 create the LocalQueryServiceConnection 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 LocalQueryServiceConnection class Public void onServiceConnected (ComponentName name, IBinder service) // The method is a callback Method !!!! After a connection is established between the client and the service, the public IBinder onBind (Intent intent) () method // returns an IBinder object to the client. this IBinder object is the service in method // public void onServiceConnected (ComponentName, IBinder service) // 4 call the method in the service using the IBinder object returned by the server // note: // 1 the conn object is used for binding and unbinding. Its class implements the ServiceConnection interface. // 2 note that you should not only rewrite the onCreate () of MainActivity to establish a connection with the service, and rewrite the onDestroy () method of the Activity! So that when the Activity exits, close the connection with the service // 3. Why do I need to 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. // But generally, internal classes are private. Therefore, in MainActivity, it is impossible to add an internal class object to receive this object. // Therefore, an interface is defined here ---> used to receive the Binder object. in this way, the business is abstracted as an interface. public class MainActivity extends Activity {TextView numberTextView; TextView resultTextView; Button button; LocalQueryServiceConnection conn = new LocalQueryServiceConnection (); ILocalQuery localBinder; // The Binder object public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setCont EntView (R. layout. main); numberTextView = (TextView) findViewById (R. id. number); resultTextView = (TextView) findViewById (R. id. result); button = (Button) findViewById (R. id. button); button. setOnClickListener (new ButtonOnClickListener (); Intent service = new Intent (this, LocalQueryService. class); // use Intent to activate the service bindService (service, conn, this. BIND_AUTO_CREATE); // start the service when the Activity starts} private class ButtonOnClic KListener implements OnClickListener {public void onClick (View v) {String number = numberTextView. getText (). toString (); String result = localBinder. queryByNumber (Integer. valueOf (number); resultTextView. setText (result) ;}// rewrite the onDestroy () method of the Activity to close the connection to the service when the Activity exits. protected void onDestroy () {unbindService (conn ); super. onDestroy ();} // receives the bound service and disconnects the private final class LocalQueryServiceConnection im Plements ServiceConnection {public void onServiceConnected (ComponentName, IBinder service) {localBinder = (ILocalQuery) service;} public void onServiceDisconnected (ComponentName name) {// when the Activity executes onDestroy, this method calls localBinder = null ;}}// server: package cn.com. service; import android. app. service; import android. content. intent; import android. OS. binder; import android. OS. IBinder; // Local SERVICE Summary: // 1 Custom Service class Local QueryService inherits from Service // 2 to write a custom internal class LocalQueryBinder to implement the business interface, and inherits from Binder // 3 to override the public IBinder onBind (Intent intent) method of the Service, returns an IBinder object. that is, the LocalQueryBinder class Object public class LocalQueryService extends Service {private String [] names = new String [] {"Xiao Ming", "Xiao Wang", "Xiao Yang", "Xiao Li "}; localQueryBinder localQueryBinder = new LocalQueryBinder (); @ Overridepublic IBinder onBind (Intent intent) {return localQueryBind Er;} // queryByNumber is the method in the service. Generally, the service is abstracted as an interface and then implemented, for example, here. Private final class LocalQueryBinder extends Binder implements ILocalQuery {// Binder implements the IBinder interface public String queryByNumber (int number) {return query (number ); // call the internal method of the Service} public String query (int I) {// internal method of the service if (I> 0 & I <5) {return names [I-1];} return "query error. Enter" ;}}// API: package cn.com. service; public interface ILocalQuery {public String queryByNumber (int number );}

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.