In this tutorial we will explain how to implement Android service lifecycle.
Service: is an long-runing application component in background and dose not provide user-interface.
the service other application component service can be start automatically after OnStart service used this method onStartCommand().
Service make a call two way
- saterted;-startService()
- bound:-BindService()
The Basice android service lifecycle-
- onStartCommand()-
Request that the service be started, by call starService().via this method,executed the service started in application background.
- onBind()-
this method when another component to bind with the service by calling bindService(). provide an interface that client used to communicate with the service by an bBinder .
- onCreate()-
this method when the service is first created to proceed one time. before it call onStartCommend() or onBind() and this method already run in background.
- onDestroy()-
package com.example.androidbeginnerpoint;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MainActivity extends Service {
int bStartMode; // indicates how to behave if the service is killed
IBinder bBinder; // interface for clients that bind
boolean bAllowRebind; // indicates whether onRebind should be used
@Override
public void onCreate() {
// The service is being created
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService
return bStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService
return bBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService
return bAllowRebind;
}
@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService
// after onUnbind has already been called
}
@Override
public void onDestroy() {
// The service is no longer used and is being destroyed
}
}
0 comments