Last active
November 23, 2018 13:57
-
-
Save diefferson/72c537147a39454045dde1861c95abcc to your computer and use it in GitHub Desktop.
Start Service Android O
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
| fun startService(context: Context){ | |
| try { | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| context.startForegroundService(Intent(context, MyService::class.java)) | |
| } else { | |
| context.startService(Intent(context, MyService::class.java)) | |
| } | |
| }catch (e :Exception){ | |
| e.stackTrace | |
| } | |
| } | |
| override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| startForeground(MyService.SERVICE_NOTIFICATION_ID, getNotificationService()) | |
| } | |
| doAnything() | |
| return super.onStartCommand(intent, flags, startId) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment