Services in Android

Services are seprate entity which is being run in background of the app. It have no effect on the front end of the app activity(functionality).

• A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application).  This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.
• A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing  connection to be made to the service in order to interact with it.

Here we implement LocalService for calling some sync task which is being required for particular app. Service class is created by extends Service. Override method of Services is as : onBind(Intent i), onCreate(), onDestroy() and onStart(Intent intent, int startid).


import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.ylog.backGroundServices.SyncTableUsers;
import com.ylog.backGroundServices.SyncVehicleLog;
import com.ylog.misc.ApplicationClass;


public class LocalService extends Service {
private static Boolean flag = false;
private ApplicationClass app;

Below method is used to bind with any particular intent.

@Override
public IBinder onBind(Intent intent) {
Log.i("TAG ", intent.getAction());
return null;
}

Below method is used to Create the service. As it is part of Service life cycle.

@Override
public void onCreate() {
Log.i("info", "onCreate");
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
flag = true;
}
Below method is used to Destroy or stop the service. As it is part of Service life cycle.

@Override
public void onDestroy() {
Log.i("info", "onDestroy");
flag = false;
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
}

Below method is used to Start service. Here we implement the task which we want to be run it on the app background.
Here we use 3 timer for calling particular function after every fixed duration of time.

@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startid) {
app = (ApplicationClass) intent.getSerializableExtra("app");
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Timer timer = new Timer();
final SyncTableUsers syncUserObj = new SyncTableUsers(LocalService.this);
final SyncVehicleLog vLogObj = new SyncVehicleLog(LocalService.this);

Below method will execute after 10 millisecond as service is started, and call after every 1.30 minute.

TimerTask timerTask = new TimerTask() {

@Override
public void run() {
if (flag) {
syncUserObj.syncUserData();
}
}
};
timer.scheduleAtFixedRate(timerTask, 10, 1800000); // call after
// every 1.30 minute

Below method will execute after 10 millisecond as service is started, and call after every 10 second.


if (app.getType_name().equalsIgnoreCase("DRIVER")) {
TimerTask timerTask1 = new TimerTask() {

@Override
public void run() {
if (flag) {
int logId = vLogObj.insertInTblVehicleLog();
}
}
};
timer.scheduleAtFixedRate(timerTask1, 10, 10000); // call
// after 10 sec
}

Below method will execute after 10 second as service is started, and call after every 1minute.


if (app.getType_name().equalsIgnoreCase("DRIVER")) {
TimerTask timerTask2 = new TimerTask() {

@Override
public void run() {
if (flag) {
vLogObj.syncVehicleLog();
}
}
};
timer.scheduleAtFixedRate(timerTask2, 10000, 60000); // call after 1 minute
}
super.onStart(intent, startid);
}
}

For start service we call it as:
Intent i = new Intent(Context, LocalService.class);
startService(i);
For using service it require a enry in Android mainfeat file
// Below android:name is package name where service class is created
< service
android:name="com.webservice.LocalService" android:enabled="true" >
< /service>

0 comments:

Post a Comment