Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to bind a service with android components .

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 243
    Comment on it

    Sometimes when we are working with service then we want the service to communicate with android components like activity.We need to accomplish this task to bind service to an activity.This type of service called android bound service.Once service has bind to activity then it can return results back to activity from bind service.Here are some defined steps through which you will come to know how to bind a service to an activity and get results back to activity.

    Step-1 Create activity class that bind with service class by using of some override methods.

    1. import com.example.androidservicebindng.MyService.MyBinder;
    2. import android.app.Activity;
    3. import android.content.ComponentName;
    4. import android.content.Context;
    5. import android.content.Intent;
    6. import android.content.ServiceConnection;
    7. import android.os.Bundle;
    8. import android.os.IBinder;
    9.  
    10. import android.view.View;
    11. import android.view.View.OnClickListener;
    12. import android.widget.Button;
    13. import android.widget.TextView;
    14.  
    15.  
    16.  
    17. public class MainActivity extends Activity {
    18. private MyService mBoundService;
    19. private boolean mServiceBound = false;
    20. private Button getDataFromServiceButton,stopServiceButton;
    21. private TextView messageTextView;
    22.  
    23. @Override
    24. protected void onCreate(Bundle savedInstanceState) {
    25. super.onCreate(savedInstanceState);
    26. setContentView(R.layout.activity_main);
    27. messageTextView = (TextView) findViewById(R.id.messageTextView);
    28. getDataFromServiceButton = (Button) findViewById(R.id.startService);
    29. stopServiceButton = (Button) findViewById(R.id.stop_service);
    30.  
    31.  
    32.  
    33. // when click this button then fetch data from MyService class
    34. getDataFromServiceButton.setOnClickListener(new OnClickListener() {
    35. @Override
    36. public void onClick(View v) {
    37. if (mServiceBound) {
    38. messageTextView.setText(mBoundService.getData());
    39. }
    40. }
    41. });
    42.  
    43. stopServiceButton.setOnClickListener(new OnClickListener() {
    44. @Override
    45. public void onClick(View v) {
    46. if (mServiceBound) {
    47. unbindService(mServiceConnection);
    48. mServiceBound = false;
    49. }
    50. Intent intent = new Intent(MainActivity.this,
    51. MyService.class);
    52. stopService(intent);
    53. }
    54. });
    55.  
    56. }
    57.  
    58. @Override
    59. protected void onStart() {
    60. super.onStart();
    61.  
    62. // start service first then bind with current activity
    63. Intent intent = new Intent(this, MyService.class);
    64. startService(intent);
    65. bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    66. }
    67.  
    68. @Override
    69. protected void onStop() {
    70. super.onStop();
    71. if (mServiceBound) {
    72. unbindService(mServiceConnection);
    73. mServiceBound = false;
    74. }
    75. }
    76.  
    77.  
    78.  
    79. /* ServiceConnection class is used to establish or bind a connection to bind service through which return
    80. * an object of MyService class hence we can operate a function of service class;
    81. */
    82.  
    83. private ServiceConnection mServiceConnection = new ServiceConnection() {
    84.  
    85. @Override
    86. public void onServiceDisconnected(ComponentName name) {
    87. mServiceBound = false;
    88. }
    89.  
    90. @Override
    91. public void onServiceConnected(ComponentName name, IBinder service) {
    92. MyBinder myBinder = (MyBinder) service;
    93. mBoundService = myBinder.getService();
    94. mServiceBound = true;
    95. }
    96. };
    97. }

    Step-2 Create a layout to this class which have two buttons startService and stopService .

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:background="#FFFFFF"
    6. android:orientation="vertical" >
    7.  
    8. <Button
    9. android:id="@+id/startService"
    10. android:layout_width="wrap_content"
    11. android:layout_height="wrap_content"
    12. android:layout_marginTop="20dp"
    13. android:text="Start Service" />
    14.  
    15. <Button
    16. android:id="@+id/stop_service"
    17. android:layout_width="wrap_content"
    18. android:layout_height="wrap_content"
    19. android:text="Stop Service" />
    20.  
    21. <TextView
    22. android:id="@+id/messageTextView"
    23. android:layout_width="wrap_content"
    24. android:layout_height="wrap_content"
    25. android:layout_marginTop="20dp"
    26. android:textAppearance="?android:attr/textAppearanceLarge" />
    27.  
    28. </LinearLayout>

    Step-3 Now defined a service class which will be bound to an activity.

    1. import android.app.Service;
    2. import android.content.Intent;
    3. import android.os.Binder;
    4. import android.os.IBinder;
    5. import android.util.Log;
    6.  
    7. public class MyService extends Service {
    8. private static String LOG_TAG = "MyService";
    9. private IBinder mBinder = new MyBinder();
    10.  
    11.  
    12. @Override
    13. public void onCreate() {
    14. super.onCreate();
    15. Log.v(LOG_TAG, "in onCreate");
    16.  
    17. }
    18.  
    19. @Override
    20. public IBinder onBind(Intent intent) {
    21. Log.v(LOG_TAG, "in onBind");
    22. return mBinder;
    23. }
    24.  
    25. @Override
    26. public void onRebind(Intent intent) {
    27. Log.v(LOG_TAG, "in onRebind");
    28. super.onRebind(intent);
    29. }
    30.  
    31. @Override
    32. public boolean onUnbind(Intent intent) {
    33. Log.v(LOG_TAG, "in onUnbind");
    34. return true;
    35. }
    36.  
    37. @Override
    38. public void onDestroy() {
    39. super.onDestroy();
    40. Log.v(LOG_TAG, "in onDestroy");
    41.  
    42. }
    43.  
    44. public String getData() {
    45.  
    46. return "Hi ,return from MyService ";
    47.  
    48. }
    49.  
    50. public class MyBinder extends Binder {
    51. MyService getService() {
    52. return MyService.this;
    53. }
    54. }
    55. }

    // In the above service class I have defined MyBinder class which inherit Binder class which in-turn implements IBinder interface.This class has a method which returns object of this MyService class. Any android components of application would be able to access public methods of this class.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: