Sunday, April 15, 2012

Start Service from Activity


Some time we need to start a service from Android Activity..
how can we achieve this i am going to write step by step.

First of all we need to Create a simple application with an Activity

-------------------------------------------
App Name: Activity2Service
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: MyActivity
-------------------------------------------


"MyActivity.java"
 
package com.rdc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MyActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Log.v("Debug", "Activity has been Started..");
  Toast.makeText(getBaseContext(), 
                      "Activity Started", Toast.LENGTH_SHORT)
    .show();
  Intent myIntent = new Intent(getBaseContext(), MyService.class);
  startService(myIntent);
 }
}

Then Create a Service
  
package com.rdc;

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

public class MyService extends Service {

 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {
  Log.v("Debug", "Service has been Created..");
  // code to execute when the service is first created
 }

 @Override
 public void onDestroy() {
  // code to execute when the service is shutting down
 }

 // This method has been deprecated since API 5
 /*
   @Override public void onStart(Intent intent, int startid) { 
          code to execute when the service is starting up
    }*/
  

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  Log.v("Debug", "Service has been Started..");
  Toast.makeText(getBaseContext(), "Service has been Started..",
    Toast.LENGTH_SHORT).show();
  // We want this service to continue running until it's explicitly
  // stopped, so return sticky.
  return START_STICKY;
 }
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>


Don't forget to make entry in "Manifest" file for Service
  
<?xml version="1.0" encoding="utf-8"?>
<manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.rdc"
 android:versionCode="1"
 android:versionName="1.0">
 <uses-sdk android:minSdkVersion="8" />

 <application
  android:icon="@drawable/icon"
  android:label="@string/app_name">
  <activity
   android:name=".MyActivity"
   android:label="@string/app_name">
   <intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
  <service
   android:enabled="true"
   android:name=".MyService">
   <intent-filter>
    <action android:name="com.rdc.MyService">
    </action>
   </intent-filter>
  </service>

 </application>

</manifest>

Now Run Application, Toast will appear..  :)

You can download the complete source code zip file here : Activity2Service

cheers!!

I'd love to hear your thoughts!

No comments:

Post a Comment