Thursday, 20 October 2011

Simple Android Service Example

What  is Service ?

A service is a component that runs in the background to perform long-running operations without needing to interact with the user. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity.

In Activity Simply calling this methods we can Start ,Stop the Service.
we can start Service using StartService() method and stop Service using StopService() method.

In Android To do background services Android Provide one Class i.e Service Class using this class we can perform background operations. 
In Service Class we are override these below methods  Start() and Destory() methods for starting an Service and Stop an Service.



First Create Your ownService Class which Extends Service (MyOwnService .java)

package com.androidsurya.serviceexample;

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

public class MyOwnService extends Service {

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "My Own Service Started....",
Toast.LENGTH_SHORT).show();
super.onStart(intent, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "My Own Service Stop....",
Toast.LENGTH_SHORT).show();
super.onDestroy();
}

}

Register the Service in Android Manifest File using Service Tag

<service android:name=".MyOwnService" />

UI Layout(activity_layout.xml)

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ServiceUsingActivity" >

    <Button
        android:id="@+id/serviceOn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Service ON" />

    <Button
        android:id="@+id/serviceOFF"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Service OFF" />

</LinearLayout>


Android Activity Class(ServiceUsingActivity .java)

package com.androidsurya.serviceexample;

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

public class ServiceUsingActivity extends Activity {

Button serviceStart, serviceStop;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
serviceStart = (Button) findViewById(R.id.serviceOn);
serviceStart.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(new Intent(ServiceUsingActivity.this,
MyOwnService.class));
}
});
serviceStop = (Button) findViewById(R.id.serviceOFF);
serviceStop.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(new Intent(ServiceUsingActivity.this,
MyOwnService.class));

}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.service_using, menu);
return true;
}

}

Register Activity in Android Manifest File

    <activity
            android:name="com.androidsurya.serviceexample.ServiceUsingActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Output ScreenShot:

















No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...