Wednesday 3 April 2013

Service Demo in Android

Demo:


ServiceDemo Activity: 

package com.venky.servicedemo;

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

public class ServiceDemoActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button btnStartService = (Button) findViewById(R.id.btnStart);
            Button btnStopService = (Button) findViewById(R.id.btnStop);

            btnStartService.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent1 = new Intent(getApplicationContext(),
                                    MyService.class);

                        startService(intent1);
                  }
            });

            btnStopService.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent2 = new Intent(getApplicationContext(),
                                    MyService.class);
                        stopService(intent2);
                  }
            });
      }
}


2)MyService.java:
 
package com.venky.servicedemo;

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 intent) {
            // TODO Auto-generated method stub
            return null;
      }

      @Override
      public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub

            Toast.makeText(getApplicationContext(), "Service Started",
                        Toast.LENGTH_SHORT).show();

            super.onStart(intent, startId);
      }

      @Override
      public void onDestroy() {
            // TODO Auto-generated method stub

            Toast.makeText(getApplicationContext(), "Service Stopped",
                        Toast.LENGTH_SHORT).show();
            super.onDestroy();
      }

}


3)main.xml:
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="70dp"
        android:text="Service Demo"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="15dp"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp" >

        <Button
            android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Start Service"
            android:textSize="15dp" />

        <Button
            android:id="@+id/btnStop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Stop Service"
            android:textSize="15dp" />
    </LinearLayout>

</LinearLayout>


4)Download this Project Click Here
 

No comments:

Post a Comment