To interact with the Activity, the Activity must first bind Service. bound Service to the Activity, which allows other applications to bind and interact with the Activity. To provide the bound service, we must implement the onBind () callback method. This method returns a programming interface defined by an internal object. The Activity can interact with the Service. Then how should we implement it? First, we should create a MyService to inherit the Service. Then how to set. (1) In your Service, create a Binder instance and return the current Service instance and an Activity to call the public method. (2) return the Binder of this instance in the onBind () callback method. (3) create ServiceConnection in Activity, accept Binder in onServiceConnected () callback method, and get the server instance. Let's take a look at the code.
Service Code
Package com. example. f22_service02; import java. util. random; import android. app. service; import android. content. intent; import android. OS. binder; import android. OS. IBinder; import android. OS. parcel; import android. OS. remoteException; import android. util. log; public class HelloService extends Service {private MyBinder binder = new MyBinder (); public class MyBinder extends Binder {public HelloService getService () {return HelloService. this; // returns the Service instance so that the main program can call this method} @ Overrideprotected boolean onTransact (int code, Parcel data, Parcel reply, int flags) throws RemoteException {// TODO Auto-generated method stubLog. I ("TAG", "------>" + data. readInt (); Log. I ("TAG", "------>" + data. readString (); reply. writeInt (2); reply. writeString ("Rose"); return super. onTransact (code, data, reply, flags) ;}@ Overridepublic IBinder onBind (Intent intent) {// TODO Auto-generated method stubreturn binder; // return method} public int getRandom () {return new Random (). nextInt (100) ;}@ Overridepublic void onCreate () {// TODO Auto-generated method stubLog. I ("TAG", "------> Create"); super. onCreate () ;}@ Overridepublic boolean onUnbind (Intent intent) {// TODO Auto-generated method stubLog. I ("TAG", "------> Unbind"); return super. onUnbind (intent );}}
Activity Method
Package com. example. f22_service02; import com. example. f22_service02.HelloService.MyBinder; import android. OS. bundle; import android. OS. IBinder; import android. OS. parcel; import android. OS. remoteException; import android. annotation. suppressLint; import android. app. activity; import android. content. componentName; import android. content. context; import android. content. intent; import android. content. serviceConnection; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button; @ SuppressLint ("Recycle") public class MainActivity extends Activity implements OnClickListener {private Button button, button2, button3; private MyBinder binder; private HelloService helloService; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); button2 = (Button) this. findViewById (R. id. button2); button3 = (Button) this. findViewById (R. id. button3); button. setOnClickListener (this); button2.setOnClickListener (this); button3.setOnClickListener (this);} private ServiceConnection connection = new ServiceConnection () {@ Overridepublic void onServiceDisconnected (ComponentName) {// TODO Auto-generated method stubLog. I ("ACTIVITY", "---------> lose binding") ;}@ Overridepublic void onServiceConnected (ComponentName, IBinder service) {// TODO Auto-generated method stubbinder = (MyBinder) service; // bind the Service and obtain the Service instance. Then, you can call the helloService = binder method in the Service. getService (); Log. I ("ACTIVITY", "---------> bound successfully") ;};@ SuppressLint ("Recycle") @ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. button1: // bind ServiceIntent intent = new Intent (MainActivity. this, HelloService. class); bindService (intent, connection, Context. BIND_AUTO_CREATE); // The third parameter is a flag. The display option is bound to break; case R. id. button2: // implement data interaction between Activity and Service // Parcel is a container that can contain messages (data and object references) and send Parcel data = Parcel through an IBinde. obtain (); data. writeInt (helloService. getRandom (); data. writeString ("jack"); Parcel reply = Parcel. obtain (); try {binder. transact (IBinder. LAST_CALL_TRANSACTION, data, reply, 0);} catch (RemoteException e) {// TODO Auto-generated catch blocke. printStackTrace ();} Log. I ("ACTIVITY", "---- Service return value ---->" + reply. readInt (); Log. I ("ACTIVITY", "---- Service return value ---->" + reply. readString (); break; case R. id. button3: // unbindService (connection); break ;}}}