Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Use Services in Android Simple Example

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 559
    Comment on it

    Using Services In Android

    Service is an application component which runs in the background and do not have a user interface.Services can be of two forms::

    Started

    Service can be started from an application component such as Activity by calling startService(). When the Service is started it can run in the background indefinitely, even if the component that started it is destroyed.For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.

    Bound

    To make the Service Bound we need to call the bindService() method to bound the Service.Once the application is bound to the service it can offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). Multiple components can be bound to the service, when all of them unbound the service is destroyed.

    To Use the Service we must create a subClass Of Service.In your implementation, you need to override some callback methods that handle key aspects of the service lifecycle and provide a mechanism for components to bind to the service, if appropriate. The most important callback methods you should override are:

    - onStartCommand()

    - onBind()

    -onCreate()

    -onDestroy()

    *Note: If component start a service by calling startService(), then it stop itself until it stops itself with stopSelf() or another component stops it by calling stopService().

    If a component calls bindService() to create the service (and onStartCommand() is not called), then the service runs only as long as the component is bound to it. Once the service is unbound from all clients, the system destroys it.

    Samle Code:

    package com.example.serviceexample;

    import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.widget.Toast;

    public class MyService extends Service {

    private static final String TAG = "com.example.ServiceExample";
    
    @Override
      public void onCreate() {
        Log.i(TAG, "Service onCreate");
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
        Log.i(TAG, "Service onStartCommand");
        System.out.println("My service onStart Commmand");
        Toast.makeText(getApplicationContext(), "in mY service", 5).show();
    
        for (int i = 0; i < 3; i++)
        {
            long endTime = System.currentTimeMillis() + 10*1000;
            while (System.currentTimeMillis() < endTime) {
              synchronized (this) {
                  try {
                      System.out.println("MyService");
                      Toast.makeText(getApplicationContext(), "in mY service", 5).show();
                      wait(endTime - System.currentTimeMillis());
                  } catch (Exception e) {
                  }
              }
         }
         Log.i(TAG, "Service running");
         }
         return Service.START&#95;STICKY;
    }
    
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        Log.i(TAG, "Service onBind");
        return null;
    }
    
    @Override
      public void onDestroy() {
        System.out.println("My service onDestroy Commmand");
        Log.i(TAG, "Service onDestroy");
    }
    

    }

    Use the attached Files for further reference

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: