Sunday, 14 October 2018

How to model a JobService as a sticky service?

As per the target requirements to Android O, I had to re-implement a service to initialize text-to-speech object as a job-scheduled service.

My original service is implemented as follows - TTSService.java

Code for TTSService class:

package services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.support.annotation.Nullable;

public class TTSService extends Service {

private static TextToSpeech voice =null;

public static TextToSpeech getVoice() {
return voice;
}

@Nullable
@Override

public IBinder onBind(Intent intent) {
// not supporting binding
return null;
}

public TTSService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

try{

        voice = new TextToSpeech(TTSService.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(final int status) {

            }
        });
}
catch(Exception e){
    e.printStackTrace();
 }


  return Service.START_STICKY;
}

@Override
public void onDestroy() {
 clearTtsEngine();
 super.onDestroy();

}

public static void clearTtsEngine()
{
if(voice!=null)
{
    voice.stop();
    voice.shutdown();
    voice = null;
  }
 }
}

To re-implement this service as a job-scheduled service, I used the following methods in my PhoneUtils class,

public static void scheduleTTSJob(Context context) {

if(isTTSJobServiceOn(context))
{
    return;
}

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

    ComponentName serviceComponent = new ComponentName(context, TTSJobScheduledService.class);
    JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, serviceComponent);

    builder.setMinimumLatency(1 * 1000); // wait at least
    builder.setOverrideDeadline(3 * 1000); // maximum delay
    JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
    jobScheduler.schedule(builder.build());
}
}

public static boolean isTTSJobServiceOn( Context context ) {

boolean hasBeenScheduled = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

    JobScheduler scheduler = (JobScheduler) context.getSystemService( Context.JOB_SCHEDULER_SERVICE );

    for ( JobInfo jobInfo : scheduler.getAllPendingJobs() ) {
        if ( jobInfo.getId() == JOB_ID ) {
            hasBeenScheduled = true;
            break;
        }
    }
}

return hasBeenScheduled;
}

public static void stopTTSJob(Context context)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    JobScheduler scheduler = (JobScheduler) context.getSystemService( Context.JOB_SCHEDULER_SERVICE );

    for ( JobInfo jobInfo : scheduler.getAllPendingJobs() ) {
        if ( jobInfo.getId() == JOB_ID ) {

            scheduler.cancel(JOB_ID);
            break;
        }
    }
}
}

and the service is re-implemented as follows: TTSJobScheduledService class

package services;

import android.annotation.TargetApi;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.os.Build;
import android.speech.tts.TextToSpeech;

import com.crashlytics.android.Crashlytics;

import utilities.PhoneUtils;

/**
* Created by Admin on 30-Sep-18.
*/

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class TTSJobScheduledService extends JobService {

private static TextToSpeech voice =null;

public static TextToSpeech getVoice() {
    return voice;
}

@Override
public boolean onStartJob(JobParameters params) {
    startTTSEngine();
    PhoneUtils.scheduleTTSJob(getApplicationContext()); // reschedule the job
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    clearTtsEngine();
    return true;
}

private void startTTSEngine()
{
    try{
        voice = new TextToSpeech(TTSJobScheduledService.this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(final int status) {

            }
        });
    }
    catch(Exception e){
        Crashlytics.log("TTS initialization issue");
        Crashlytics.logException(e);
    }
}

private static void clearTtsEngine()
{
    if(voice!=null)
    {
        voice.stop();
        try {
            voice.shutdown();
        }
        catch (Exception e){
            Crashlytics.log("ClearTTSEngine shutdown issue");
            Crashlytics.logException(e);
        }
        voice = null;
    }
 }
}

It seemed to work well on devices with Android O, until I noticed that the service terminated after a while and did not restart on its own. To make it manually restart, I had to either open the app or restart my device.

Basically, I want to model my TTSService as a sticky, jobservice (basically, it should be restarted by the OS once it ends). For this purpose, can I just restart the service in the onStopJob() method of the TTSJobScheduledService? Or is there a better way to do this?



from How to model a JobService as a sticky service?

No comments:

Post a Comment