Creating your Own Services
The best way to understand how a service works is by creating one. The following example shows you the steps to create a simple service.
For now, you will learn how to start and stop a service.
Creating a Simple Service
1 . Using Eclipse, create a new Android project and name it Services.
2 . Add a new class file to the project and name it MyService.java. Populate it with the following code:
package com.emergingandroidtech.Services;
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)
{
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
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();
}
}
3 . In the AndroidManifest.xml file, add the following statement in bold:
<?xml version=”1.0”encoding=”utf-8”?>
<manifest
xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.emergingandroidtech.Services”
android:versionCode=”1”
android:versionName=”1.0”>
<application
android:icon=”@drawable/icon”
android:label=”@string/app_name”>
<activity
android:name=”.MainActivity”
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:name=”.MyService” />
</application>
<uses-sdk
android:minSdkVersion=”9”/>
</manifest>
4 . In the main.xml file, add the following statements in bold:
<?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” >
<Button
android:id=”@+id/btnStartService”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Start Service” />
<Button
android:id=”@+id/btnStopService”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Stop Service” />
</LinearLayout>
5 . Add the following statements in bold to the MainActivity.java file:
package com.emergingandroidtech.Services;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity
{
/** Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnStart = (Button) findViewById(R.id.btnStartService);
btnStart.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startService(new Intent(getBaseContext(), MyService.class));
}
});
Button btnStop = (Button) findViewById(R.id.btnStopService);
btnStop.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
stopService(new Intent(getBaseContext(), MyService.class));
}
});
}
}
6 . Clicking the Start Service button will start the service. To stop the service, click the Stop Service button.
nice tutorial bro...keep it up
ReplyDelete