Introduction Of Service
Forms Of Services
Started
A service is started when an application component (such as an activity) start it by calling startService(). once started, a service run in background indefinitely. even if the component that started it is destroyed.
For example- download the file from network, when operation is done, service stop itself.
Bounded
A service is bounded when another component (e.g. client) calls bindService() method.the client can unbind the service by calling the unbindService() method.
Life Cycle Of Service
When we creating service, we have to make sub class of Service. We have to override the some callback methods of service.
Important callbacks methods are:
onStartCommand() - This method called when the component,such as activity called startService() method and service started. Once service be started it executes indefinitely in background. we have to explicitly stop the service when work is done, by call the stopSelf() or stopService() method.
onBind() - This method called when another component bind the service by calling bindService() method. In your implementation of this method, you have to provide an interface that users use to communicate with the service, by returning an IBinder. You must always implement this method, but if you don't want to allow binding,then you should return null.
onCreate() - This method called when the service called first time created. Here all the service initialization done. If service is already running this method is not called.
onDestroy() - This method called when the service is no longer used and is being destroyed This method is used to,clean up any resources such as threads,registered listeners,receivers,etc.
Example - Here I am implementing a Service that play song in background. Implementation of Service
When we create a service, We have to extends Service Class or one of the subclass and need to declaration in the AndroidManifest.xml.
<service
android:name="program.androidheight.servicedemo.MyBoundedService"
android:enabled="true"
android:exported="true" >
</service>
activity_main.xml
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="33dp"
android:text="PlaySong" />
<Button
android:id="@+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnStart"
android:layout_alignRight="@+id/btnStart"
android:layout_below="@+id/btnStart"
android:layout_marginTop="67dp"
android:text="StopSong" />
</RelativeLayout>
Here I am Implementing Service
MyBoundedService.java
package program.androidheight.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
/**
* Created by PRABHU on 17-05-2015.
*/
public class MyBoundedService extends Service {
MediaPlayer player;
public MyBoundedService() {
}
@Override
public void onCreate() {
super.onCreate();
player=MediaPlayer.create(MyBoundedService.this, R.raw.song);
Toast.makeText(MyBoundedService.this, "Service Created!", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(MyBoundedService.this, "Service Started!", Toast.LENGTH_LONG).show();
player.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
player.release();
Toast.makeText(MyBoundedService.this, "Service Destroyed!", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="program.androidheight.servicedemo" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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="MyBoundedService"
android:enabled="true"
android:exported="true" >
</service>
<service
android:name="program.androidheight.servicedemo.MyBoundedService"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>
- Service is an android component that performs long running operations in the background without any user interface.
- A Service can not directly communicate to an activity. A component can bind to a service to interact with it.
- A service always performs single operations and stops itself once task completed.
- A Services runs in the main thread of the application instance. It doesn't create its own Thread. If your service is going to do any long running blocking operation it might cause Application Not Responding(ANR), Hence you need to create a new Thread inside Service.
- Example of Services are: play music, downloading mp3 song or video from server etc.
Forms Of Services
Started
A service is started when an application component (such as an activity) start it by calling startService(). once started, a service run in background indefinitely. even if the component that started it is destroyed.
For example- download the file from network, when operation is done, service stop itself.
Bounded
A service is bounded when another component (e.g. client) calls bindService() method.the client can unbind the service by calling the unbindService() method.
Life Cycle Of Service
When we creating service, we have to make sub class of Service. We have to override the some callback methods of service.
Important callbacks methods are:
onStartCommand() - This method called when the component,such as activity called startService() method and service started. Once service be started it executes indefinitely in background. we have to explicitly stop the service when work is done, by call the stopSelf() or stopService() method.
onBind() - This method called when another component bind the service by calling bindService() method. In your implementation of this method, you have to provide an interface that users use to communicate with the service, by returning an IBinder. You must always implement this method, but if you don't want to allow binding,then you should return null.
onCreate() - This method called when the service called first time created. Here all the service initialization done. If service is already running this method is not called.
onDestroy() - This method called when the service is no longer used and is being destroyed This method is used to,clean up any resources such as threads,registered listeners,receivers,etc.
Example - Here I am implementing a Service that play song in background. Implementation of Service
When we create a service, We have to extends Service Class or one of the subclass and need to declaration in the AndroidManifest.xml.
<service
android:name="program.androidheight.servicedemo.MyBoundedService"
android:enabled="true"
android:exported="true" >
</service>
activity_main.xml
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="33dp"
android:text="PlaySong" />
<Button
android:id="@+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnStart"
android:layout_alignRight="@+id/btnStart"
android:layout_below="@+id/btnStart"
android:layout_marginTop="67dp"
android:text="StopSong" />
</RelativeLayout>
MainActivity.java
package program.androidheight.servicedemo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button btnStart,btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart=(Button)findViewById(R.id.btnStart);
btnStop=(Button)findViewById(R.id.btnstop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnStart:
startService(new Intent(MainActivity.this,MyBoundedService.class));
break;
case R.id.btnstop:
stopService(new Intent(MainActivity.this,MyBoundedService.class));
break;
}
}
}
Here I am Implementing Service
MyBoundedService.java
package program.androidheight.servicedemo;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
/**
* Created by PRABHU on 17-05-2015.
*/
public class MyBoundedService extends Service {
MediaPlayer player;
public MyBoundedService() {
}
@Override
public void onCreate() {
super.onCreate();
player=MediaPlayer.create(MyBoundedService.this, R.raw.song);
Toast.makeText(MyBoundedService.this, "Service Created!", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(MyBoundedService.this, "Service Started!", Toast.LENGTH_LONG).show();
player.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
player.release();
Toast.makeText(MyBoundedService.this, "Service Destroyed!", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="program.androidheight.servicedemo" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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="MyBoundedService"
android:enabled="true"
android:exported="true" >
</service>
<service
android:name="program.androidheight.servicedemo.MyBoundedService"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>
You can download the code from here : github
0 comments:
Post a Comment