Created
January 8, 2018 16:14
-
-
Save nazmulidris/91dee865330a7d9a55cc2f332148ac38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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