Skip to content

Instantly share code, notes, and snippets.

@nazmulidris
Created January 8, 2018 16:14
Show Gist options
  • Select an option

  • Save nazmulidris/91dee865330a7d9a55cc2f332148ac38 to your computer and use it in GitHub Desktop.

Select an option

Save nazmulidris/91dee865330a7d9a55cc2f332148ac38 to your computer and use it in GitHub Desktop.
public class MyService extends Service{
private void commandStart() {
if (!mServiceIsStarted) {
moveToStartedState();
return;
}
if (mExecutor == null) {
mTimeRunning_sec = 0;
if (isPreAndroidO()) {
HandleNotifications.PreO.createNotification(this);
} else {
HandleNotifications.O.createNotification(this);
}
mExecutor = Executors
.newSingleThreadScheduledExecutor();
Runnable runnable =
new Runnable() {
@Override
public void run() {
recurringTask();
}
};
mExecutor.scheduleWithFixedDelay(
runnable, DELAY_INITIAL,
DELAY_RECURRING, DELAY_UNIT);
d(TAG, "commandStart: starting executor");
} else {
d(TAG, "commandStart: do nothing");
}
}
@TargetApi(Build.VERSION_CODES.O)
private void moveToStartedState() {
Intent intent = new MyIntentBuilder(this)
.setCommand(Command.START).build();
if (isPreAndroidO()) {
Log.d(TAG, "moveToStartedState: on N/lower");
startService(intent);
} else {
Log.d(TAG, "moveToStartedState: on O");
startForegroundService(intent);
}
}
@Override
public int onStartCommand(
Intent intent, int flags, int startId) {
boolean containsCommand = MyIntentBuilder
.containsCommand(intent);
d(TAG,
String.format(
"Service in [%s] state. id: [%d]. startId: [%d]",
mServiceIsStarted ? "STARTED" : "NOT STARTED",
containsCommand ?
MyIntentBuilder.getCommand(intent) : "N/A",
startId));
mServiceIsStarted = true;
routeIntentToCommand(intent);
return START_NOT_STICKY;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment