Example of connecting / unlinking (android)

Can you give me a simple example of an application with a background service that uses bind / unbind methods to start and stop? I worked with him on searching for an hour and a half, but these examples use the startService / stopService methods or itโ€™s very difficult for me. thank.

+44
android service binding
Dec 01 2018-11-12T00:
source share
3 answers

You can try using this code:

protected ServiceConnection mServerConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { Log.d(LOG_TAG, "onServiceConnected"); } @Override public void onServiceDisconnected(ComponentName name) { Log.d(LOG_TAG, "onServiceDisconnected"); } } public void start() { // mContext is defined upper in code, I think it is not necessary to explain what is it mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE); mContext.startService(intent); } public void stop() { mContext.stopService(new Intent(mContext, ServiceRemote.class)); mContext.unbindService(mServerConn); } 
+55
Dec 01 '11 at 13:09
source share

Add these methods to your work:

 private MyService myServiceBinder; public ServiceConnection myConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { myServiceBinder = ((MyService.MyBinder) binder).getService(); Log.d("ServiceConnection","connected"); showServiceData(); } public void onServiceDisconnected(ComponentName className) { Log.d("ServiceConnection","disconnected"); myService = null; } }; public Handler myHandler = new Handler() { public void handleMessage(Message message) { Bundle data = message.getData(); } }; public void doBindService() { Intent intent = null; intent = new Intent(this, BTService.class); // Create a new Messenger for the communication back // From the Service to the Activity Messenger messenger = new Messenger(myHandler); intent.putExtra("MESSENGER", messenger); bindService(intent, myConnection, Context.BIND_AUTO_CREATE); } 

And you can bind to the service using ovverriding onResume () and onPause () in your Activity class.

 @Override protected void onResume() { Log.d("activity", "onResume"); if (myService == null) { doBindService(); } super.onResume(); } @Override protected void onPause() { //FIXME put back Log.d("activity", "onPause"); if (myService != null) { unbindService(myConnection); myService = null; } super.onPause(); } 

Note that when binding to a service in the service class, only the onCreate() method is onCreate() . In your Service class, you need to define the myBinder method:

 private final IBinder mBinder = new MyBinder(); private Messenger outMessenger; @Override public IBinder onBind(Intent arg0) { Bundle extras = arg0.getExtras(); Log.d("service","onBind"); // Get messager from the Activity if (extras != null) { Log.d("service","onBind with extra"); outMessenger = (Messenger) extras.get("MESSENGER"); } return mBinder; } public class MyBinder extends Binder { MyService getService() { return MyService.this; } } 

After you have defined these methods, you can use the methods of your service in your activity:

 private void showServiceData() { myServiceBinder.myMethod(); } 

and finally, you can start your service when some kind of event happens, for example _BOOT_COMPLETED _

 public class MyReciever extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals("android.intent.action.BOOT_COMPLETED")) { Intent service = new Intent(context, myService.class); context.startService(service); } } } 

note that when the onCreate() and onStartCommand() services are started, it is called in the service class and you can stop your service when another event occurs using stopService (); note that your event listener must be registered in your Android manifest file:

 <receiver android:name="MyReciever" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> 
+35
Dec 27 '12 at 15:31
source share

First of all, two things we need to understand

Client

  • Executes a request to a specific server

bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);

here mServiceConn is an instance of the ServiceConnection class (built-in), it is actually an interface that we need to implement using two (1st for the network connected and 2nd network not connected) method for monitoring the status of the network connection.

Server

  • It processes the clientโ€™s request and makes its own copy, which is private only for clients who send the request, and this raplica server runs in different threads.

Now on the client side, how to access all server methods?

  • The server sends a response using the IBinder Object. So, the IBinder object is our handler that accesses all Service methods using the (.) Operator.

.

 MyService myService; public ServiceConnection myConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { Log.d("ServiceConnection","connected"); myService = binder; } //binder comes from server to communicate with method of public void onServiceDisconnected(ComponentName className) { Log.d("ServiceConnection","disconnected"); myService = null; } } 

Now, how to call the method that is in the service

 myservice.serviceMethod(); 

Here myService is an object, and the serviceMethod method is a method. and in this way a connection is established between the client and the server.

+13
Aug 14 '14 at 12:54 on
source share



All Articles