Service: is one of core component of Android.
Service have not any user interface, like dialogboxe or buttons etc. Service runs in background. We use service for long process like fetch long (time consuming) data from network, play sound in background or some process where user interaction is not necessary.
For long process we can also use thread or AsyncTask but these are affected by Activity lifecycle, But Service is not affected by Activity lifecycle Service have its own life cycle.
Create new project in eclipse (Android-First-Programm).
Create new class by Right click on package that is in src folder, select new and click on Class
I write class name “AndroidService” and click on finish
For Service we must be inherit (extends) Service class
Now class name must be underline by red line. If we take cursor on class name it will ask to “Add unimplemented methods” your “AndroidService” class code will be like this, or write these methods by yourself.
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) { return null; } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "Called when service starts", Toast.LENGTH_LONG).show(); } @Override public void onCreate() { Toast.makeText(this, "Called when service created", Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { Toast.makeText(this, "Called when service stop (service should clean its resources)", Toast.LENGTH_LONG).show(); } }
For service add below bold line in Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itPearl.androidservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".AndroidService" android:enabled="true" /> </application> </manifest>
Use two buttons in layout to start or stop service, so “activity_main” will be like this
<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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startService" android:text="Start service" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="stopService" android:text="Stop service" /> </LinearLayout>
And main MainActivity will be used to start or stop service.
[sociallocker]
package com.itPearl.androidservice; import android.os.Bundle; import android.view.View; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void startService(View view){ startService(new Intent(this, AndroidService.class)); } public void stopService(View view){ stopService(new Intent(this, AndroidService.class)); } }
[/sociallocker]