Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Importance of Services in Android Application

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 783
    Comment on it

    To perform long running operations on background without any disturbance with the application we use Services in android. Services are workable even if the application is destroyed. Service base class have various callback methods onCreate(), onBind(), onUnbind(), onRebind(), onStartCommand(), and onDestroy(). Services are inherited from base class Service.


    onCreate() - This method is called when service is being created. Variables and definitions are defined in this block.
    onBind() - This method is called when other components wants to communicate/bind.
    onUnbind() - When all components disconnected from a particular interface published by the service.
    onRebind() - Called when new client have to connect with the service.
    onStartCommand() - This method is for starting and terminating the service. By calling two different method startService() and stopService() to request service start and service stop when its work is done.
    onDestroy() - When service is being destroyed and have no longer use.


    Important points about services:-
    Service is not bound with the lifecycle of any activity.
    Service is similar to activity but have no interface.
    Mostly services are used for time consuming tasks.
    Services may run even if your application activity is closed.

    public class NotifyServices extends Service{  
    
        @Override  
         public void onCreate() {  
             super.onCreate();  
         }  
    
        @Override  
         public void onDestroy() {  
             super.onDestroy();  
         }  
    
        @Override  
         public IBinder onBind(Intent intent) {  
             return null;  
         }
    
        @Override
            public boolean onUnbind(Intent intent) {
                return super.onUnbind(intent);
            }  
    
         @Override
            public void onRebind(Intent intent) {
                super.onRebind(intent);
            }
    
        @Override  
         public int onStartCommand(Intent intent, int flags, int startId) {  
             // This method gets called, once the service is started, perform operations like notifications, downloading, uploading etc...  
             // The intent holds the data passed from activity. 
             // If background operation is time consuming, then create thread here.  
         }  
    } 
    

    Example:- The below example show large-format notifications in android.

     public class NotificationService extends Service{
    
        private NotificationManagerCompat mManager;
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flag, int startid) {
    
            mManager = NotificationManagerCompat.from(this);
    
            Intent notificationIntent = new Intent(this.getApplicationContext(), MainActivity.class);
            notificationIntent.putExtra("start_id", startid);
    
            Notification notification = new Notification(R.drawable.funny_android, "See My App something for you", startid);
    
            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
            PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
            notification.flags = Notification.FLAG_GROUP_SUMMARY;
    
            //get the bitmap to show in notification bar
            Bitmap bitmap_image = BitmapFactory.decodeResource(this.getResources(), R.drawable.funny_android);
            NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    
                notification = builder.setContentIntent(pendingNotificationIntent)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Set title")
                        .setContentText("Set Content text")
                        .setTicker("Set Ticker text")
                        .setStyle(s)
                        .build();
    
            mManager.notify(startid, notification);
    
            return startid;
            }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            Logger.getLogger("Alam Services Destroyed");
            super.onDestroy();
        }
    }
    

    Use of service in android applications are:-
    Notifications
    Playing music
    Downloading files
    Handling network transactions
    Content providers etc...

 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: