Android Services


 

Introduction to service

  • A service is a application component that run in the background to perform long running operation without needing to interact with the user.
  • Service can run in the background indefinitely ,even if component that started the service is destroyed.
  • A service run in the main thread of the application instance.It does’t create its own thread.
  • Service always performing a single operation and stop itself one intended task is completed.

Service can essentially take two forms:

  • Started service-A service is started when the application is (like Activity) start it by calling startService() method.It is stopped by stopService() method.The service can stop itself by calling the stopSelf() method.
  • Bound service-A service is bound when an application component bind to its by calling bindService() method. The client can unbind the service by calling the unBindService() method.

Android Service Life Cycle

Android Services

onStartCommand()

This method is called when the service be started,by calling startService().onece this method executes, the service is started and can run in the background indefinitely

onBind()

This metod is called when another component wants to bind with the service by calling bindService().

onUnbind()

This method is called when the all clients have disconnected from a particular interface.

onCreate()

This method is called while the service is first created.Here all the service initialization is done.This method is never called again.

onDestroy()

The system call this method when the service is no longer used and being destroyed.

Creating Android Service

Right click on src/package – New – Service-Service

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Start service"
        android:id="@+id/button"
        android:onClick="startService"/>
    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Stop Service"
        android:id="@+id/button2"
        android:layout_alignParentEnd="true"
        android:onClick="stopService"/>
</RelativeLayout>

MyService.java

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

public class MyService extends Service {
    public MyService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
     
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service stop", Toast.LENGTH_LONG).show();
    }

MainActivity.java

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startService(View view){
        startService(new Intent(MainActivity.this,MyService.class));
    }
    public void stopService(View view){
        stopService(new Intent(MainActivity.this,MyService.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {    
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {     
        int id = item.getItemId();      
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Start Android Service

Intent intent= new Intent (this, MyService.class);

startService(intent);

Stop Running Android Service

A service must be stopped itself by calling stopSelf() method onece it is finish execuation. You can also stopped a service yourself by calling stopService() method.

stopService() method will call onDestroy() callback in your service.

Android Services

Posted in All Tutorials, Android Tutorials | Tagged | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Powered By: wpnova.com