Service
A service is an Android application component that run in background and has no visual UI. While the user is working on the foreground UI, services can be used to handle the processes that need to be done in the background. A service can be started by another Android application components such as an activity or other services and it will continue to run in the background even after the user switches to another application. Thus services are less likely to be destroyed by Android system to free resources, than Activities.
Ex:
example of service in android that plays an audio in the background. Audio will not be stopped even if you switch to another activity. To stop the audio, you need to stop the service.
Type of Service:
2 types
A service is an Android application component that run in background and has no visual UI. While the user is working on the foreground UI, services can be used to handle the processes that need to be done in the background. A service can be started by another Android application components such as an activity or other services and it will continue to run in the background even after the user switches to another application. Thus services are less likely to be destroyed by Android system to free resources, than Activities.
Ex:
example of service in android that plays an audio in the background. Audio will not be stopped even if you switch to another activity. To stop the audio, you need to stop the service.
Type of Service:
2 types
- Started(Unbound)
- Bound
1) Started Service
Its a type of service which is not bounded to any components. Once started, it will run in the background even after the component that started the service gets killed. It can be run in the background indefinitely and should stop by itself after the operation its intended to carry out is completed.
A service is started when component (like activity) calls startService() method, now it runs in the background indefinitely. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method.
2) Bound Service
A service is bound when another component (e.g. client) calls bindService() method. The client can unbind the service by calling the unbindService() method.
The service cannot be stopped until all clients unbind the service.
Ex: Playing an Music
Step 1: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="#FFFFFF" >
<Button
android:id="@+id/buttonStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="19dp"
android:text="Start Service" />
<Button
android:id="@+id/buttonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:text="Stop Service" />
<Button
android:id="@+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Page" />
</LinearLayout>
Step 2: activity_next.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="#FFFFFF" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="96dp"
android:layout_marginTop="112dp" android:textColor="#000000"
android:text="Next Page" />
</LinearLayout>
Step 3: MainActivity.java
package com.example.servicesex;
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 MainActivity extends Activity implements OnClickListener {
Button buttonStart, buttonStop,buttonNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonNext = (Button) findViewById(R.id.buttonNext);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonNext.setOnClickListener(this);
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.buttonStart:
startService(new Intent(getApplicationContext(), MyService.class));
break;
case R.id.buttonStop:
stopService(new Intent(getApplicationContext(),MyService.class));
break;
case R.id.buttonNext:
Intent intent=new Intent(this,NextPage.class);
startActivity(intent);
break;
}
}
}
Step 4: MyService.java
package com.example.servicesex;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service{
MediaPlayer mPlayer;
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onBind...", 2000).show();
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(getApplicationContext(), "oncreate", 2000).show();
mPlayer=MediaPlayer.create(this,R.raw.tuhimerishab);
mPlayer.setLooping(false);
mPlayer.start();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(getApplicationContext(), "onStart", 2000).show();
//mPlayer.start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("ondestoryee");
Toast.makeText(getApplicationContext(), "onDestroy", 2000).show();
mPlayer.stop();
}
}
Step 5: NextStep.java
package com.example.servicesex;
import android.app.Activity;
import android.os.Bundle;
public class NextPage extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
}
}
Step 6: Add service tag in AndroidManifest.java
<service android:name=".MyService" android:enabled="true"></service>
<activity android:name=".NextPage"/>
Great tutorial!!!
ReplyDeleteI was looking how to start service and
this is the best solution I found.
One question, is it possible for service to be running even if you kill activity or to start just service without activity.
Check out : http://www.bestandroidtrainingchennai.in/android-blog/android-services-tutorial/
Thanks in advance