Monday, May 2, 2016

Android Services

In this blog I am taking a brief idea about services and few example in android service is also a component and it is a not small topics if we going to understand in details

Android Services

A service is a component that runs in the background to perform long-running operations without needing to interact with the user and it works even if application is destroyed.
There is two types of android services available in android

Start Services:

A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.
Ex:
package com.example.TestService;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
  
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }

   // Method to start the service
   public void startService(View view) {
      startService(new Intent(getBaseContext(), MyService.class));
   }

   // Method to stop the service
   public void stopService(View view) {
      stopService(new Intent(getBaseContext(), MyService.class));
   }
}



Service:
package com.example. TestService;

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

public class MyService extends Service {
  
   @Override
   public IBinder onBind(Intent arg0) {
      return null;
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      // Let it continue running until it is stopped.
      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      return START_STICKY;
   }
   
   @Override
   public void onDestroy() {
      super.onDestroy();
      Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
   }
}


Bound Services:

This service used to bind a services with activity.If the activity wants to interact with the service directly, it can use the bindService() method to start the service.
This method requires a ServiceConnection object as a parameter which is called on the service start and when finishing its onBind() method. This method returns a IBinder object to the ServiceConnection.
This IBinder object can be used by the activity to communicate with the service.
When the binding process has finished, the onStartCommand() method in the service is called with the Intent object used for the bindService() method
There is two more services used in android Intent Services and Foreground services.
Intent Services:

It extend the IntentService class for  service implementation.
The IntentService is used to perform a certain task in the background. Once done, the instance of IntentServiceterminates itself automatically. An example for its usage would be downloading certain resources from the internet.
The IntentService class offers the onHandleIntent() method which will be asynchronously called by the Android system.

Foreground services:

A foreground service is a service that should have the same priority as an active activity and therefore should not be killed by the Android system, even if the system is low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

Syntax to start a service as a foreground services:


Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), System.currentTimeMillis());
Intent notificationIntent = new Intent(this, TestActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);






No comments:

Post a Comment